gpt4 book ai didi

javascript - 如何让我的图像以随机顺序循环?

转载 作者:行者123 更新时间:2023-12-04 08:51:13 25 4
gpt4 key购买 nike

我知道 math.random() 选项存在,但我无法弄清楚如何在我的代码中使用它。目前,它以排序方式循环。

(function () {
var rotator = document.getElementById("rotator"); // change to match image ID
var imageDir =
"/Users/loh/Documents/ElectronJS/newapp1/newapp1/pages/images/visionboard/";
var delayInSeconds = 1;
var num = 0;
let currentIndex = 0;
const totalImages = 16;
const changeImage = function () {
num = ++num % totalImages;
rotator.src = imageDir + num + ".png";
};
setInterval(changeImage, delayInSeconds * 1000);
})();

最佳答案

(function () {
var rotator = document.getElementById("rotator"); // change to match image ID
var imageDir =
"/Users/loh/Documents/ElectronJS/newapp1/newapp1/pages/images/visionboard/";
var delayInSeconds = 1;
var num = 0;
let currentIndex = 0;
const totalImages = 16;
const changeImage = function () {
var incre = Math.floor(Math.random() * (totalImages-1) ) + 1;
num += incre;
num = num % totalImages;
rotator.src = imageDir + num + ".png";
};
setInterval(changeImage, delayInSeconds * 1000);
})();

关于javascript - 如何让我的图像以随机顺序循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64094567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com