2014-09-23 93 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