我试图在随机时间加载随机图像。出于某种原因,这并没有随机化时间,尽管它确实随机化了图像。有什么问题吗?
var randomTime2 = 3000; //initialize the random time with this value to start
setInterval(function(){
var randomNum = Math.floor((Math.random()*3)+1);//random num for image
$('.recent-project2 img').fadeTo('slow',0,function(){
var randomImg = "img/recent-projects-"+randomNum+".jpg";
$('.recent-project2 img').attr('src',randomImg);
$('.recent-project2 img').fadeTo('slow',1);
});
randomTime2 = Math.floor(Math.random()*10000);//reset random time
return randomTime2; //return random time
},randomTime2);
最佳答案
使用 setTimeout
并在函数结束时以随机时间重新触发。
关于javascript - 创建一个时间间隔,使函数在 JavaScript 中随机运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17220730/