Javascript math.round阈值

时间:2014-09-23 22:11:18

标签: javascript html

我正在跟踪一个我希望以5秒的间隔触发的计时器。问题是 用我的

测试
if (Math.round(time.position) % 5 === 0){do stuff}

它会触发太多次。它将触发所有9.~十进制值。 如何以5秒的间隔触发它?

time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 9.29} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 9.43} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 9.54} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 9.68} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 9.79} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 9.92} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 10.03} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 10.18} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 10.29} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 10.42} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 10.54} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 10.68} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 10.79} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 10.92} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 11.04} index.html:104
time:  Object {duration: 149.49, type: "jwplayerMediaTime", position: 11.17} 

1 个答案:

答案 0 :(得分:3)

我相信这会每5秒触发一次。

var previousValue = 1;
if(Math.ceil(time.position / 5) !== previousValue) {
    previousValue = Math.ceil(time.position / 5);
}

请确保您只拨打第一行。

相关问题