【发布时间】:2021-07-31 10:10:33
【问题描述】:
我正在尝试设计一个循环函数,从我的 html 中提取图像并将它们放置在屏幕上的随机位置。到目前为止,它正在工作。但是,我希望它随机地反复更改图像和位置。现在,代码在加载时运行一次,并且保持静态。我一直在尝试使用循环,但到目前为止它们要么不起作用,要么使页面崩溃。
window.onload = function() {
randomPlace1('parameter');
};
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
var idNum = (getRandomInt(86));
setTimeout("randomPlace1()", 30000);
var i = 0;
function randomPlace1() {
setTimeout(function() {
document.getElementById("i" + idNum).style.display = "block";
document.getElementById("i" + idNum).style.top = Math.floor(Math.random() * 101);
document.getElementById("i" + idNum).style.left = Math.floor(Math.random() * 101);
document.getElementById("i" + idNum).style.zIndex = Math.floor(Math.random() * 10);
}, 3000);
}
【问题讨论】:
-
你应该使用 setInteval 而不是 setTimeout
-
这对于移动图像非常有用,非常棒。我感谢您的帮助!我现在只需要图像也不断变化。我会继续努力的。
标签: javascript loops web-deployment infinite-loop repeat