gpt4 book ai didi

Javascript math.round 阈值

转载 作者:行者123 更新时间:2023-11-28 19:35:15 31 4
gpt4 key购买 nike

我正在跟踪一个我想以 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 math.round 阈值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26005508/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com