Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                    
                
        

我一直在跟踪要以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}

最佳答案

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

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


但是请确保只拨打第一行。

关于javascript - JavaScript math.round阈值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26005508/

10-11 02:39