js动画实现&&回调地狱&&promise
1. js实现动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>animate</title>
<style>
.ball {
width: 40px;
height: 40px;
margin-bottom: 5px;
border-radius: 20px;
}
.ball1 {
background: red;
}
.ball2 {
background: blue;
}
.ball3 {
background: yellow;
}
</style>
</head>
<body>
<div class="ball ball1" style="margin-left: 0"></div>
<div class="ball ball2" style="margin-left: 0"></div>
<div class="ball ball3" style="margin-left: 0"></div>
<script>
var ball1 = document.querySelector(".ball1");
var ball2 = document.querySelector(".ball2");
var ball3 = document.querySelector(".ball3");
function animate(ball, left, callback) {
setTimeout(function () {
var marginLeft = parseInt(ball.style.marginLeft, );
if (marginLeft === left) {
callback && callback();
} else {
if (marginLeft < left) {
marginLeft += ;
} else {
marginLeft -= ;
}
ball.style.marginLeft = marginLeft + "px";
animate(ball, left, callback);
}
}, );
}
animate(ball1, , function () {
animate(ball2, , function () {
animate(ball3, , function () {
animate(ball1, , function () {
animate(ball3, , function () {
animate(ball2, , function () {
animate(ball2, , function () {
animate(ball2, , function () {
console.log("over");
})
})
})
})
})
})
})
});
</script>
</body>
</html>
上述代码就可以实现一个动画。注意下面几点:
- 动画的实现往往依赖于setTimeout。
- 注意ele.style.marginLeft如果开始能够获取,必须从元素的style中设置了才能获取,否则获取不到。
- 利用callback可以实现虽然使用了setTimeout还能串行执行。
但是这产生了回调地狱,代码简单点还好说,一旦代码复杂了,我们将很难处理其中的逻辑。所以这时就可以用到es6中的promise了。
Promise的写法如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>animate</title>
<style>
.ball {
width: 40px;
height: 40px;
margin-bottom: 5px;
border-radius: 20px;
}
.ball1 {
background: red;
}
.ball2 {
background: blue;
}
.ball3 {
background: yellow;
}
</style>
</head>
<body>
<div class="ball ball1" style="margin-left: 0"></div>
<div class="ball ball2" style="margin-left: 0"></div>
<div class="ball ball3" style="margin-left: 0"></div>
<script>
var ball1 = document.querySelector(".ball1");
var ball2 = document.querySelector(".ball2");
var ball3 = document.querySelector(".ball3"); function promiseAnimate(ball, left) {
return new Promise(function (resolve, reject) {
function animate(ball, left) {
setTimeout(function () {
var marginLeft = parseInt(ball.style.marginLeft, );
if (marginLeft === left) {
resolve();
} else {
if (marginLeft < left) {
marginLeft += ;
} else {
marginLeft -= ;
}
ball.style.marginLeft = marginLeft + "px";
animate(ball, left);
}
}, );
}
animate(ball,left);
});
}
promiseAnimate(ball1, )
.then(function () {
return promiseAnimate(ball2, );
})
.then(function () {
return promiseAnimate(ball3, );
})
.then(function () {
return promiseAnimate(ball1, );
})
.then(function () {
return promiseAnimate(ball3, );
})
.then(function () {
return promiseAnimate(ball2, );
})
.then(function () {
return promiseAnimate(ball2, );
})
.then(function () {
return promiseAnimate(ball2, );
}) </script>
</body>
</html>
这同样可以达到效果,并且这样做的好处是,修改更加容易一些。对于promise,有几点需要注意:
- 在执行promise相关函数的时候,要返回一个promise对象,这是常用的做法。
- 只有返回了promise对象,我们才能实用then。
- 并且在then中还要返回promise对象,这样我们就可以不断的使用then()来管理异步。
- 在promise执行之后,要使用resolve()来表明这个promise执行的结束。 这样,才能执行then方法。
问题: 在then中如果直接执行promiseAnimate(ball2, 200);不可以吗? 为什么一定要return呢?
答: 当然不可以,因为如果直接执行,确实返回了一个promise对象,但是这个promise对象只是在then下面的函数中啊, 我们必须在这个函数继续返回这个promise对象才能达到继续使用then的目的。
其中resolve()代表着这个异步过程的结束。
综上所述: 动画多用setTimeout和调用自己的方式执行,当然,使用setInterval也是一样的,只是前者我们更为推荐。 无论是使用setTimeout还是setInterval,都不可避免的会产生如果解决异步的问题。 之前我们解决异步的方式是使用回调函数,但是回调函数非常容易就会产生回调地狱,所以用promise会更好一些。
js动画实现&&回调地狱&&promise的更多相关文章
- js中的回调地狱 Callback to Hell
本文重点:解决方式:1.promise 2. 拆解 function:将各步拆解为单个的 function 3. 通过 Generator 函数暂停执行的效果方式 4. 通过ES8的异步函 ...
- js中的回调函数 和promise解决异步操作中的回调地狱问题。
回调函数 : 函数作为参数传递到另外一个函数中.简单数据类型和引入数据类型中的数组和对象作为参数传递大家肯定都不陌生,其实引用数据类型中的函数也是可以的. 事实上大家见到的很多,用到的也很多,比如jQ ...
- js中promise解决callback回调地狱以及使用async+await异步处理的方法
1.callback回调地狱 function ajax(fn) { setTimeout(()=> { console.log('你好') fn() }, 1000) } ajax(() =& ...
- 避免Node.js中回调地狱
为了解决这个阻塞问题,JavaScript严重依赖于回调,这是在长时间运行的进程(IO,定时器等)完成后运行的函数,因此允许代码执行经过长时间运行的任务. downloadFile('example. ...
- 【JavaScript】 使用Async 和 Promise 完美解决回调地狱
很久以前就学习过Async和Promise,但总是一知半解的. 今天在写NodeJS的时候,发现好多第三方库使用回调,这样在实际操作中会出现多重回调,这就是传说中的JS回调地狱. 举个例子 有一个方法 ...
- ES6(promise)_解决回调地狱初体验
一.前言 通过这个例子对promise解决回调地狱问题有一个初步理解. 二.主要内容 1.回调地狱:如下图所示,一个回调函数里面嵌套一个回调函数,这样的代码可读性较低也比较恶心 2.下面用一个简单的例 ...
- Promise如何解决回调地狱
为什么要有promise:解决(回调地狱)的问题 ### 回调地狱: ```js //跟以前的if条件地狱很像 // if(){ // if(){ // if(){ // } // } //} $.g ...
- Promise解决回调地狱(多层调用问题)
Promise # Promise 是异步编程的一种解决方案:从语法上讲,promise是一个对象,从它可以获取异步操作的消息:从本意上讲,它是承诺,承诺它过一段时间会给你一个结果.promise有三 ...
- [JS]回调函数和回调地狱
回调函数 小明在奶茶店点了奶茶,店员开始制作奶茶,此时"制作奶茶"与"小明等待奶茶"是一个同时进行的不同的两个事件(任务),那么,小明获取店员制作成功的奶茶是从 ...
随机推荐
- 设计模式04: Factory Methord 工厂方法模式(创建型模式)
Factory Methord 工厂方法模式(创建型模式) 从耦合关系谈起耦合关系直接决定着软件面对变化时的行为 -模块与模块之间的紧耦合使得软件面对变化时,相关的模块都要随之变更 -模块与模块之间的 ...
- String类的subtring(,)
截取字符串,参数(起始位置,截取长度)
- window phone 8资源管理器打开文件
一.打开安装包里面文件: StorageFolder sf = Package.Current.InstalledLocation;//ApplicationData.Current.LocalFol ...
- C# 筛选string 类型里面的汉字,获取首字母字母,正则表达式Regex 常用验证
界面效果 1.提取汉字 private void buttonX1_Click(object sender, EventArgs e) { if (TxtYuan.Text.Trim() != &qu ...
- kubernetes dashboard 安装
环境:CentOS Linux release 7.3.1611 (Core)IP:192.168.0.103 [1]组件安装yum install device-mapperyum install ...
- VSCODE 针对调试C语言时一闪而过解决办法
针对调试C语言时一闪而过解决办法 前提: 已经按照 C/C++ 已经安装 MINGW(并配置完成) 原因: 主要是因为tasks的配置没有写对 解决办法: tasks.json { // See h ...
- C# LINQ(4)
where作为LINQ的条件关键字. where的右面是表达式 表达式可以方法返回值,但是where的右面最终是一个可检测真假的表达式 代码: static void Main(string[] ar ...
- mysql 行转列,对列的分组求和,对行求和
CREATE TABLE students( id INT PRIMARY KEY, NAME VARCHAR(11)); CREATE TABLE courses( id INT PRIMARY K ...
- ES聚合报错
在测试Elasticsearch聚合的时候报了一个错误.具体如下: GET /megacorp/employee/_search { "aggs": { "all_int ...
- 写excel
一.写excel import xlwt book = xlwt.Workbook()# 创建excel sheet = book.add_sheet('stu_info')# 加一个sheet sh ...