Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>五种异步流程控制的实现</h1>
<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>
              
            
!

CSS

              
                .ball{
  width: 40px;
  height: 40px;
  margin-bottom: 20px;
  border-radius: 50%;
}
.ball1{
  background-color: red;
}
.ball2{
  background-color: teal;
}
.ball3{
  background-color: deeppink;
}
              
            
!

JS

              
                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)
}
// promise 版的动画实现
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()
  })
}

//方法一,利用回调函数实现
// animate(ball1, 100, function () {
//   animate(ball2, 200, function () {
//     animate(ball3, 300, function () {
//       animate(ball3, 150, function () {
//         animate(ball2, 150, function () {
//           animate(ball1, 150, function () {

//           })
//         })
//       })
//     })
//   })
// })

// 方法二 利用promise实现
// promiseAnimate(ball1,100)
//   .then(function () {
//     return promiseAnimate(ball2,200)
//   })
//   .then(function () {
//     return promiseAnimate(ball3,300)
//   })
//   .then(function () {
//     return promiseAnimate(ball3,150)
//   })
//   .then(function () {
//     return promiseAnimate(ball2,150)
//   })
//   .then(function () {
//     return promiseAnimate(ball1,150)
//   })

// 方法三 利用 async await 实现
// (async function () {
//   await promiseAnimate(ball1, 100)
//   await promiseAnimate(ball2, 200)
//   await promiseAnimate(ball3,150)
//   await promiseAnimate(ball2,150)
//   await promiseAnimate(ball1,150)
// })()

// 方法四 基于 promise 的 generator 实现
// function run(gen){
//   var g = gen();

//   function next(data){
//     var result = g.next(data);
//     if (result.done) return result.value;
//     result.value.then(function(data){
//       next(data);
//     });
//   }

//   next();
// }

// var movePromise = function* (){
//   yield promiseAnimate(ball1, 100);
//   yield promiseAnimate(ball2, 200);
//   yield promiseAnimate(ball3, 150);
//   yield promiseAnimate(ball2, 150);
//   yield promiseAnimate(ball1, 150);
// };
// run(movemovePromise)

// 方法五 基于 thunk 的 generator 实现
function thunkify(fn) {
  return function() {
    var args = new Array(arguments.length);
    var ctx = this;

    for (var i = 0; i < args.length; ++i) {
      args[i] = arguments[i];
    }

    return function (done) {
      var called;

      args.push(function () {
        if (called) return;
        called = true;
        done.apply(null, arguments);
      });

      try {
        fn.apply(ctx, args);
      } catch (err) {
        done(err);
      }
    }
  }
};

var animateThunk = thunkify(animate)

function run(fn) {
  var gen = fn();

  function next(value) {
    console.log(value)
    var result = gen.next();
    if (result.done) return;
    result.value(next);
  }

  next();
}

var moveThunk = function* (){
  yield animateThunk(ball1, 100)
  yield animateThunk(ball2, 200)
  yield animateThunk(ball3, 150)
  yield animateThunk(ball2, 150)
  yield animateThunk(ball1, 150)
};

run(moveThunk)
              
            
!
999px

Console