简体   繁体   English

如何在每 30 秒后更改 Javascript 中的变量值

[英]how to change the variable value in Javascript after every 30 seconds

i have an array like我有一个像

  var A=["2","4","5"];

I have a set of value in one array.我在一个数组中有一组值。 But i need show value only by one between 30 seconds.但我只需要在 30 秒之间显示一个值。 Can anyone help me.谁能帮我。

get each value of items in callback after 30 seconds 30 秒后获取回调中项目的每个值

 function getValue(items, cb, i) { i = i || 0; if (i < items.length) { setTimeout(function() { cb(items[i]) i++; getValue(items, cb, i); }, 30 * 1000); } } getValue(['1', 2, 3], function(val) { console.log(val); });

You are looking for the setInterval() method.您正在寻找setInterval()方法。

 (function() { var source = ["2", "4", "5"]; var delay = 1000; // use 30000 for 30 seconds var currentIndex = 0; var A = source[currentIndex]; // Starting value window.console.log(A); // demo var intervalId = setInterval(function() { currentIndex += 1; A = source[currentIndex]; window.console.log(A); // demo // Clear interval if (source.length === currentIndex + 1) { clearInterval(intervalId); } }, delay); })();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM