javascript - 在 animate settimeout 之后执行函数

标签 javascript jquery html css jquery-animate

前几天在堆栈溢出时发现了这个 http://codepen.io/anon/pen/LERrGG .

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

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

(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)
})();

最佳答案

您可以通过检查 theta 最终是否比开始时小来确定是否绘制了一个完整的圆圈:

(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);
  }
})();

关于javascript - 在 animate settimeout 之后执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27678873/

相关文章:

javascript - 使用 JavaScript 和 jQuery 显示多级嵌套 JSON 数据

javascript - 如何为 mediaelement.js 创建皮肤?

html - 绝对定位的 div 的宽度

html - Gmail 将某些 HTML 元素包装在一个名为 im 的类中

javascript - Ionic 3 - ionic 选择(多个)在选择两个项目后禁用所有选项

javascript - 如何在我的网站中使用 NPM 的模块源?

javascript - 使用 Vue 增强现有的多页面/服务器渲染/经典 Web 应用程序

html - 如何更改 Bootstrap 滚动 spy 事件链接颜色

Javascript/ECMAScript 垃圾收集

javascript - 组件渲染过早