【问题标题】:How to do when 'countdown timer' has run out and then run the function 'alert()' in 'Momentjs'当'倒数计时器'用完然后在'Momentjs'中运行函数'alert()'时怎么办
【发布时间】:2017-05-16 02:49:13
【问题描述】:

这里我使用 Codeigniter 和 MomentJS,如何在 'contdown timer' 用完时运行 'alert()' 函数

在 Main.JS 中

var timeEvent = <?=$timeEvent?>;
var timeNow = <?=now('America/Santiago')?>;
var diff = timeEvent - timeNow;
var duration = moment.duration(diff*1000, 'milliseconds');
var interval = 1000;

setInterval(function(){
  duration = moment.duration(duration - interval, 'milliseconds');
  $('#result').text(duration.hours() + ":" + duration.minutes() + ":" + duration.seconds());
}, interval);

【问题讨论】:

  • 在设置持续时间后放置 if 语句并检查持续时间的值,如果它
  • 顺便说一句,请注意 setInterval() 不能保证在指定的时间间隔运行,因此如果您正在显示倒计时,通常的做法是在计算中考虑当前时间而不是而不是每次都减去相同的值。

标签: javascript jquery codeigniter momentjs


【解决方案1】:

就这么简单吗?

setInterval(function(){
  duration = moment.duration(duration - interval, 'milliseconds');
  if( duration <= 0 ) {
    alert("Done");
  }
  $('#result').text(duration.hours() + ":" + duration.minutes() + ":" + duration.seconds());
}, interval);

【讨论】:

  • 谢谢,它的工作:)
  • 对了,如何添加 'clearInterval' ?是不是就是这样 if( duration
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多