【问题标题】:Infinite Looping Javascript function, random imageInfinite Looping Javascript 函数,随机图像
【发布时间】: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


【解决方案1】:

尝试改用 setInterval...

.setTimeout() //executes the code after x seconds.
.setInterval() //executes the code **every** x seconds.

【讨论】:

    猜你喜欢
    • 2021-06-28
    • 2017-04-23
    • 2021-04-15
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2021-08-17
    • 2013-03-06
    相关资源
    最近更新 更多