I am programming a web page, which will a different message every 4 seconds. I have built a custom object which I intend to pull information from to create the strings to be displayed. I’m using a function similar to Math.random called ‘rand’ which will let me access the object randomly. I’ve set the interval for 4 seconds. I can get the string to be displayed, but after that the web page continues to load and load and begins to lag so badly that I have to exit out. The code I’m using is shown below. Please help!
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);
http://jsfiddle.net/aHrpC/2/
This JSFiddle has a working example. It had to do with document.write. Everything else in your code is working correctly.