想通过回调函数做一个动画效果:三个小球依次运动,第一个小球运动到指定位置后回调第二个小球运动,依次类推,效果如图所示:

到第三个小球到达指定位置再回调,让第二个小球往回移动,直到最后一个小球回到原位:

具体实现过程使用了简单的定时器,代码如下:

        var ball1 = document.querySelector('.ball1');
        var ball2 = document.querySelector('.ball2');
        var ball3 = document.querySelector('.ball3');

        function animate(ball, distance, cb) {
            setTimeout(function() {
                var marginLeft = parseInt(ball.style.marginLeft, 10);
                if (marginLeft === distance) {
                    cb && cb();
                }
                else {

                    if (marginLeft < distance) {
                        marginLeft ++;
                    }
                    else {
                        marginLeft --;
                    }

                    ball.style.marginLeft = marginLeft + 'px';
                    animate(ball, distance, cb)
                }
            },13)
        }

回调函数:

        animate(ball1, 100, function() {
            animate(ball2, 100, function() {
                animate(ball3, 100, function() {
                    animate(ball3, 0, function() {
                        animate(ball2, 0, function() {
                            animate(ball1, 0, function() {
                                alert(666)
                            })
                        })
                    })
                })
            })
        })

看着这阶梯一般的回调函数,强迫症患者可能会很爽,但如果有成百上千的回调函数,这将会是一场灾难;

现在我们用promise重新写一下上面的函数。

首先安装bluebird,防止浏览器不兼容promise。

promise暴露在全局,可以直接var Promise = window.Promise;

重写promiseAnimate函数:

function promiseAnimate(ball, distance) {
            return new Promise(function(resolve, reject) {
                function _animate() {
                    setTimeout(function() {
                        var marginLeft = parseInt(ball.style.marginLeft, 10);
                        if (marginLeft === distance) {
                            resolve()
                        }
                        else {

                            if (marginLeft < distance) {
                                marginLeft ++;
                            }
                            else {
                                marginLeft --;
                            }

                            ball.style.marginLeft = marginLeft + 'px';
                            _animate();
                        }
                    },13)
                }
                _animate();
            })
        }

使用promise的then方法回调:

promiseAnimate(ball1, 100)
            .then(function() {
                return promiseAnimate(ball2, 100)
            })
            .then(function() {
                return promiseAnimate(ball3, 100)
            })
            .then(function() {
                return promiseAnimate(ball3, 0)
            })
            .then(function() {
                return promiseAnimate(ball2, 0)
            })
            .then(function() {
                return promiseAnimate(ball1, 0)
            })

使用then后,我们可以链式的回调,一目了然,简洁清晰!

当然promise还有很多特性,今天只是初步尝试一下~

最新文章

  1. SecWeek
  2. Eclipse中为自己写完的函数添加注释(快捷键ALT+SHIFT+J)
  3. jQuery -&gt; bind / live / delegate 的终结者 - on
  4. 「C语言」文件的概念与简单数据流的读写函数
  5. Unichar, char, wchar_t
  6. !! Android developer 最新国内镜像
  7. js动态增加表格
  8. 手机APP与原生APP设计的区别
  9. A Tour of Go Making slices
  10. 关于LZO和LZOP
  11. 1、第一个SpringMVC程序
  12. CountDownLatch类的使用
  13. day5_ 导入模块和包
  14. Django—urls系统:urls基础
  15. easyui时间框只选择年月
  16. 插入与归并(python)(原创)
  17. Ch07 包和引入 - 练习
  18. 第一章 Html+Css使用总结(下)
  19. 《Software Design中文版01》
  20. junit中test注解测试使用案列解析一

热门文章

  1. VMware Linux 下 Nginx
  2. C和Java中数组的定义
  3. Ubuntu12.04环境搭建遇到的问题和建议(一个)
  4. Android 实现用户列表信息的功能,然后选择删除幻灯片删除功能
  5. ajax 请求数据
  6. PBKDF2WithHmacSHA1算法
  7. [QT Creator]LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 。
  8. ArcGIS 10.1 发布使用ArcEngine自定义的GP服务
  9. worker进程中线程的分类及用途
  10. 6 MySQL视图