JS动画中利用Promise解决回调地狱的实例解析
本文通过JS动画实例,对比分析了使用setTimeout配合回调函数与ES6 Promise处理异步任务的差异。详细讲解了Promise链式调用的实现方式、resolve()的作用以及必须return Promise对象的原因,旨在帮助开发者理解如何避免回调地狱,提升代码可维护性。
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, 10);
if (marginLeft === left) {
callback && callback();
} else {
if (marginLeft < left) {
marginLeft += 2;
} else {
marginLeft -= 2;
}
ball.style.marginLeft = marginLeft + "px";
animate(ball, left, callback);
}
}, 13);
}
animate(ball1, 100, function () {
animate(ball2, 200, function () {
animate(ball3, 300, function () {
animate(ball1, 200, function () {
animate(ball3, 200, function () {
animate(ball2, 180, function () {
animate(ball2, 220, function () {
animate(ball2, 200, 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, 10);
if (marginLeft === left) {
resolve();
} else {
if (marginLeft < left) {
marginLeft += 2;
} else {
marginLeft -= 2;
}
ball.style.marginLeft = marginLeft + "px";
animate(ball, left);
}
}, 13);
}
animate(ball,left);
});
}
promiseAnimate(ball1, 500)
.then(function () {
return promiseAnimate(ball2, 200);
})
.then(function () {
return promiseAnimate(ball3, 300);
})
.then(function () {
return promiseAnimate(ball1, 200);
})
.then(function () {
return promiseAnimate(ball3, 200);
})
.then(function () {
return promiseAnimate(ball2, 180);
})
.then(function () {
return promiseAnimate(ball2, 220);
})
.then(function () {
return promiseAnimate(ball2, 200);
})
</script>
</body>
</html>
这种方式同样能达到预期效果,且其优势在于后续修改更加便捷。对于Promise的使用,有几点需要特别注意:
1. 在执行与Promise相关的函数时,应当返回一个Promise对象,这是常规做法。 2. 只有返回了Promise对象,我们才能调用then方法。 3. 并且在then回调中也要返回Promise对象,这样我们就可以通过不断链式调用then()来管理异步流程。 4. 在Promise执行完毕后,必须使用resolve()来表明该Promise执行结束,从而触发then方法的执行。
问题: 在then中如果直接执行promiseAnimate(ball2, 200);不可以吗? 为什么一定要return呢?
答: 当然不可以。因为如果直接执行,虽然确实返回了一个Promise对象,但这个对象仅仅存在于当前then内部的函数作用域中。我们必须在这个函数中继续返回这个Promise对象,才能达到继续使用then进行链式调用的目的。
其中resolve()代表着这个异步过程的结束。
综上所述: 动画多用setTimeout和调用自己的方式执行,当然,使用setInterval也是一样的,只是前者我们更为推荐。 无论是使用setTimeout还是setInterval,都不可避免的会产生如果解决异步的问题。 之前我们解决异步的方式是使用回调函数,但是回调函数非常容易就会产生回调地狱,所以用promise会更好一些。
总结
以上所述是小编给大家介绍的JS动画实现回调地狱promise的实例代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对本站网站的支持!
JavaScript异步发送HTTP请求时怎么优雅地处理错误
2026-11-05 17:10:44
5款主流JS在线运行工具对比:CodePen、JSFiddle与StackBlitz怎么选
2026-11-05 17:04:04
JavaScript入门书籍避坑:这些书不适合初学者
2026-11-05 16:31:15
JS使用Math.round实现保留两位小数的原理与代码
2026-11-05 15:35:55
JavaScript 原生 Date 对象怎么实现日期加减
2026-11-05 14:30:00
