js简单倒计时

忘是亡心i 2022-04-04 20:27 381阅读 0赞

不想每次用倒计时,都现写代码,比较烦,这里记一下,也顺便分享一些倒计时简单的逻辑。

如果你有更简单方便的代码,可以分享给大家。

  1. var method = {
  2. countdownObj: {
  3. timer: null,
  4. changeTime: 0,
  5. },
  6. countdown: function(long, back) {
  7. var that = this;
  8. if (that.countdownObj.timer) {
  9. clearInterval(that.countdownObj.timer);
  10. }
  11. that.countdownObj.changeTime = long;
  12. back(that.countdownObj.changeTime);
  13. that.countdownObj.timer = setInterval(function() {
  14. that.countdownObj.changeTime--;
  15. back(that.countdownObj.changeTime);
  16. if (that.countdownObj.changeTime < 1) {
  17. clearInterval(that.countdownObj.timer);
  18. }
  19. }, 1000);
  20. }
  21. };
  22. method.countdown(60,function(time){
  23. console.log(time);
  24. });

函数里第一个数字是到时间长度,
第二个回调函数,回传的time就是当前时间。

勘误:
1018-12-12 修正了几个文字错误;优化了几个变量

原文地址:https://segmentfault.com/a/1190000017341822

发表评论

表情:
评论列表 (有 0 条评论,381人围观)

还没有评论,来说两句吧...

相关阅读

    相关 js简单计时

    涉及的知识点       setTime() setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式。 1.提示: 1000 毫秒= 1 秒。 2.提示

    相关 js简单计时

    不想每次用倒计时,都现写代码,比较烦,这里记一下,也顺便分享一些倒计时简单的逻辑。 如果你有更简单方便的代码,可以分享给大家。 var method = {