Javascript 在设置动画超时后执行函数

Javascript 在设置动画超时后执行函数,javascript,jquery,html,css,jquery-animate,Javascript,Jquery,Html,Css,Jquery Animate,前几天在堆栈溢出上发现了这个 我认为这是一支非常有用的钢笔。唯一的问题是,计时器用完后无法调用函数。我试图实现这一点,但没有成功 如何编辑代码,使其成为有用的计时器,即“用完” (function animate() { theta += 0.5; theta %= 360; var x = Math.sin(theta * Math.PI / 180) * radius; var y = Math.cos(theta * Math.PI / 180) * -radius;

前几天在堆栈溢出上发现了这个

我认为这是一支非常有用的钢笔。唯一的问题是,计时器用完后无法调用函数。我试图实现这一点,但没有成功

如何编辑代码,使其成为有用的计时器,即“用完”

(function animate() {
  theta += 0.5;
  theta %= 360;
  var x = Math.sin(theta * Math.PI / 180) * radius;
  var y = Math.cos(theta * Math.PI / 180) * -radius;
  var d = 'M0,0 v' + -radius + 'A' + radius + ',' + radius + ' 1 ' + ((theta > 180) ? 1 : 0) + ',1 ' + x + ',' + y + 'z';
  timer.setAttribute('d', d);
  setTimeout(animate, t)
})();

你必须检查θ是否小于360

  (function animate() {
  theta += 0.5;
  var x = Math.sin(theta * Math.PI / 180) * radius;
  var y = Math.cos(theta * Math.PI / 180) * -radius;
  var d = 'M0,0 v' + -radius + 'A' + radius + ',' + radius + ' 1 ' + ((theta > 180) ? 1 : 0) + ',1 ' + x + ',' + y + 'z';
  timer.setAttribute('d', d);
 if(theta<360) setTimeout(animate, t);
 else doSomething();
})();
(函数animate(){
θ+=0.5;
var x=数学sin(θ*数学π/180)*半径;
变量y=数学cos(θ*数学π/180)*-半径;
变量d='M0,0V'+-radius+'A'+radius+','A'+radius+'1'+((θ>180)?1:0)+',1'+x+','y+'z';
timer.setAttribute('d',d);

如果(θ您可以通过检查θ是否小于开始时的值来确定绘制了一个完整的圆:

(function animate() {
  var oldTheta = theta;
  theta += 0.5;
  theta %= 360;
  if (theta < oldTheta) {
    // the timer has "run out"
  }
  else {
    var x = Math.sin(theta * Math.PI / 180) * radius;
    var y = Math.cos(theta * Math.PI / 180) * -radius;
    var d = 'M0,0 v' + -radius + 'A' + radius + ',' + radius + ' 1 ' + ((theta > 180) ? 1 : 0) + ',1 ' + x + ',' + y + 'z';
    timer.setAttribute('d', d);
    setTimeout(animate, t);
  }
})();
(函数animate(){
var oldTheta=θ;
θ+=0.5;
θ%=360;
if(θ<旧θ){
//计时器已“耗尽”
}
否则{
var x=数学sin(θ*数学π/180)*半径;
变量y=数学cos(θ*数学π/180)*-半径;
变量d='M0,0V'+-radius+'A'+radius+','A'+radius+'1'+((θ>180)?1:0)+',1'+x+','y+'z';
timer.setAttribute('d',d);
设置超时(动画,t);
}
})();

这是一个“正常的”
setTimeout()
。以编写此代码的方式,计时器从不“耗尽”.你能澄清你的问题吗?那么我该如何让它用完,以便计时器可以使用?这意味着什么?你必须准确地解释你想做什么,否则没有人能帮你。你的意思是你想在循环完成后做些什么?我对javascript特别是动画非常不擅长。(我的背景是服务器分析)我只是在胡闹,认为有一个有用的计时器会很酷。不幸的是,我不太明白sittimeout()在这个例子中是如何使用的。如果thea的值高于360,它就不应该调用setTimeout函数。哦,我明白你的意思了。对不起:)