简体   繁体   English

如何每秒第二次从变量中移除一定次数的JavaScript?

[英]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.

相关问题 如何在反应中每秒将变量降低某个值 - How do I lower a variable by a certain value every second in react 在 Code.org HTML Web Lab 中,当 Javascript 变量达到某个数字时,如何为该数字着色? - How do I colour a number from a Javascript variable when it hits a certain number, within the Code.org HTML Web Lab? 如何在JS中舍入一个数字并取走一个数字? - How do I round a number down and take away a number in JS? 如何从 html 数字输入中创建数字变量? - How do I make a number variable from a html number input? 如何每1秒钟使用javascript递增一个数字? - How can I increment a number after every 1 second using javascript? 我如何从用户那里获取输入引用并对其进行洗牌,然后在 JavaScript 中将每个字母分开放在一个表格中 - How do i take the input quote from a user and shuffle it then put every letter separate in a table in JavaScript 您如何从html文本框中获取输入并将其分配给javascript变量? - How do you take input from a html textbox and assign it to a javascript variable? 我如何使用 JavaScript 将输入从文本框中获取到变量然后打印变量的值? - How do i use JavaScript to take the input from a text box to a variable then print the value of a variable? 如何用 Javascript 将字符串中的每隔一个逗号替换为空格? - How do I replace every second comma in a string with a space with Javascript? 我正在创建一个每秒更改字体的标题,但是如何更改变量以将其设置为另一种字体? CSS/JavaScript - I am creating a heading which changes font every second, but how to do i change the variable to set it to another font? css/javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM