gpt4 book ai didi

javascript - 随时间变化的变量数组

转载 作者:行者123 更新时间:2023-12-01 03:27:35 25 4
gpt4 key购买 nike

我想创建一个包含变量的数组,这些变量应该在给定时间后自行更改。这是我的代码:

var cookieArray =  new Array(window.setInterval(myCallback, 2000));

function myCallback() {
cookie1 = Math.floor((Math.random() * 810) + 0);
cookie2 = Math.floor((Math.random() * 810) + 0);
cookie3 = Math.floor((Math.random() * 810) + 0);
cookie4 = Math.floor((Math.random() * 810) + 0);
cookie5 = Math.floor((Math.random() * 810) + 0);
cookie6 = Math.floor((Math.random() * 810) + 0);
cookie7 = Math.floor((Math.random() * 810) + 0);
cookie8 = Math.floor((Math.random() * 810) + 0);
cookie9 = Math.floor((Math.random() * 810) + 0);
cookie10 = Math.floor((Math.random() * 810) + 0);
}

最佳答案

您正在创建一个数组,但没有初始化它。要初始化数组,您应该在您的情况下使用 arrayName[0]=value

 var cookieArray =[];

window.setInterval(myCallback, 2000);

function myCallback() {
cookieArray[0] = Math.floor((Math.random() * 810) + 0);
cookieArray[1] = Math.floor((Math.random() * 810) + 0);

console.log(cookieArray);
}

关于javascript - 随时间变化的变量数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44735253/

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