[英]How do I take away from a variable a certain number of times every second javascript/html?
I am trying to subtract a certain amount from variable every second. 我试图每秒从变量中减去一定量。 How do I do that?
我怎么做? When I search 'how to take away from a variable javascript', two things show up, how to delete a variable, and how to take away a certain number off a variable all at once.
当我搜索“如何从变量javascript中删除”时,会显示两件事,如何删除变量,以及如何一次从变量中删除一定数量的变量。 So I just searched up, how to say 'per second' in javascript and I would try to figure the other part on my own and I could not find that either.
因此,我只是搜索了一下,如何在javascript中说“每秒”,我会尝试自己计算另一部分,但我也找不到。
Thanks in advance! 提前致谢!
~John 〜约翰
You typically use setInterval
for this, so... 您通常为此使用
setInterval
,因此...
var intervalId = setInterval(
function() {
// This function will be executed once every second (give or take)
// You can subtract from your variable here
// If you're done running, you can stop the interval using...
clearInterval(intervalId);
}, 1000, this);
var x = 10;
function reduce() {
console.log(x);
x = x-1;
if (x === 0) return;
setTimeout(reduce, 1000);
}
reduce();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.