我想在玩家玩游戏时每秒获取当前时间,但我希望它是整数,但我从来没有得到精确的数字。 对,不,我正在使用 Math.round() 和 Math.ceil() 来解决这个问题,但有时我会得到相同的数字显示两次,因为它有时会以错误的方式四舍五入数字。
function checkstop() {
if(done == true ){
setTimeout(checkstop, 1000);
currenttime = player.getCurrentTime();
$('.holderrs').html(Math.round(currenttime));
}
}
播放时输出:
1、2、3、3、5、6、7、7、9、10、11、12、13、13、15...
最佳答案
setTimeout
仅保证 checkstop
函数将在您的情况下在 1000 毫秒后调用不少于
。所以,我想,最好使用较小的时间段。例如:
var previousValue;
function checkstop() {
if (done === true) {
currenttime = Math.round(player.getCurrentTime());
if (previousValue !== currenttime) { // but set new value only if previous value is not equal new value
previousValue = currenttime;
$('.holderrs').html(currenttime);
}
setTimeout(checkstop, 100); // call after each 100 milliseconds
}
}
关于javascript - YouTube 的 APi : "current time" isn't precise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35995209/