我正在编写一个网页,它每 4 秒会发出一条不同的消息。我已经构建了一个自定义对象,我打算从中提取信息以创建要显示的字符串。我正在使用一个类似于 Math.random 的函数,称为“rand”,它可以让我随机访问对象。我已将间隔设置为 4 秒。我可以显示要显示的字符串,但之后网页继续加载和加载并开始严重滞后,以至于我不得不退出。我正在使用的代码如下所示。请帮忙!
var name = "Barack Obama";
var Actions = {
"ActionList" : [
{ "action" : "watches a 30 minute TV show",
"time" : 30,
"time_measure" : "minute",
"assuming" : "assuming he sat through the commercials too"
},
{
"action" : "makes some pizza rolls",
"time" : 9,
"time_measure" : "minute",
"assuming" : "he only made one plateful"
},
{
"action" : "takes a 2 hour nap",
"time" : 2,
"time_measure" : "hour",
"assuming" : "assuming he's a heavy sleeper"
},
{
"action" : "1000 jumping jacks",
"time" : 1,
"time_measure" : "hour",
"assuming" : "he doesn't take a break, or slow down"
}
]
};
function rand(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function run() {
/**
* RANDOMLY SELECT OBJECTS
***/
var r = rand(0, 3);
var x = Actions.ActionList[r]["action"];
document.write( x );
}
setInterval(run, 4000);