为了账号安全,请及时绑定邮箱和手机立即绑定

我用Jquery的deferred对象为什么实现不了异步编程?

<!doctype html>

<html>

<head>

<meta charset="UTF-8">

<title>小球运动+递归函数</title>

<style type="text/css">

.ball{

width: 40px;

height: 40px;

border-radius: 50%;

margin-top: 10px;

}

#ball1{

     background: red;

}

#ball2{

     background: yellow;

}

#ball3{

     background: blue;

}

</style>

</head>

<body>

<div class="ball" id = "ball1"  style="margin-left:10px"></div>

<div class="ball" id = "ball2"  style="margin-left:10px"></div>

<div class="ball" id = "ball3"  style="margin-left:10px"></div>



<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>

<script type="text/javascript">

window.requestAnimFrame = (function() {

return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||

function(element) {

return window.setTimeout(callback, 1000 / 60);

};

})();

  var ball1 =document.querySelector("#ball1");

  var ball2 =document.querySelector("#ball2");

  var ball3 =document.querySelector("#ball3");


 

function animate (ball,distance) {

  var dtd = $.Deferred();

 requestAnimFrame(function(){

var marginleft = parseInt(ball.style.marginLeft, 10);

if(marginleft === distance){

dtd.resolve();

}else if(marginleft < distance){

marginleft++;     

}else{

marginleft--;

}

ball.style.marginLeft = marginleft + 'px';

animate(ball,distance);

});

   return dtd;

}

       

       animate(ball1,100).then(function(){

        return animate(ball2,200);

       }).then(function(){

        return animate(ball3,300);

       }).then(function(){

        return animate(ball3,150);

       }).then(function(){

        return animate(ball2,150);

       }).then(function(){

        return animate(ball1,150);

       })

</script>


</body>

</html>


正在回答

1 回答

后来自己捣弄出来了,下面是实现代码:

<!doctype html>

<html>

<head>

<meta charset="UTF-8">

<title>小球运动+clearInterval()</title>

<style type="text/css">

.ball{

width: 40px;

height: 40px;

border-radius: 50%;

margin-top: 10px;

}

#ball1{

     background: red;

}

#ball2{

     background: yellow;

}

#ball3{

     background: blue;

}

</style>

</head>

<body>

<div class="ball" id = "ball1"  style="margin-left:10px"></div>

<div class="ball" id = "ball2"  style="margin-left:10px"></div>

<div class="ball" id = "ball3"  style="margin-left:10px"></div>



<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>

<script type="text/javascript">


  var ball1 =document.querySelector("#ball1");

  var ball2 =document.querySelector("#ball2");

  var ball3 =document.querySelector("#ball3");


 

function animate (ball,distance) {

 var timer = null;

 var dtd = $.Deferred();

 timer = setInterval(function(){

var marginleft = parseInt(ball.style.marginLeft, 10);

if(marginleft === distance){

clearInterval(timer);

dtd.resolve();

}else if(marginleft < distance){

marginleft++;     

}else{

marginleft--;

}

ball.style.marginLeft = marginleft + 'px';

},13);

   return dtd;

}

       animate(ball1,100).then(function(){

        return animate(ball2,200);

       }).then(function(){

        return animate(ball3,300);

       }).then(function(){

        return animate(ball3,150);

       }).then(function(){

        return animate(ball2,150);

       }).then(function(){

        return animate(ball1,150);

       })

</script>

</body>

</html>


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
进击Node.js基础(二)
  • 参与学习       76755    人
  • 解答问题       226    个

本教程带你攻破 Nodejs,让 JavaScript流畅运行在服务器端

进入课程

我用Jquery的deferred对象为什么实现不了异步编程?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信