2014-09-23 102 views
1

我跟踪一个计时器,我想以5秒为间隔触发。问题是 与我的Javascript math.round阈值

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} 
+0

'setInterval(function(){...},5000);'是否足够? – Mandera 2014-09-23 22:14:51

+0

不,这是由视频播放器定时器触发的。 – seasick 2014-09-23 22:15:05

+0

你必须跟踪你已经覆盖的职位。 – Pointy 2014-09-23 22:15:21

回答

3

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

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

确保您只调用第一行一次。

+0

谢谢,谢谢! – seasick 2014-09-23 22:21:56