showRandom函数每1000毫秒执行一次,但我想让它每隔几毫秒执行一次..有什么解决办法吗?谢谢!
var random = 1000;
setInterval(function() {random = randomizator(60000,200000);} ,1000);
setInterval(function() {showRandom(random);}, random);
function randomizator(a,b)
{
return Math.floor(Math.random()*b) + a;
}
function showRandom(random)
{
$('#test').text(random);
}
演示:jsFiddle
解决方法:
Felix说:如果你想每次更改间隔,请改用setTimeout.简化示例,因为我无法完全遵循您希望原始代码执行的操作:
doTheRandom();
function doTheRandom() {
random = randomizator(60000,200000);
// Up to 1 second
setTimeout(doTheRandom, randomizator(1000, 2000)); // 1-2 seconds
}