javascript - Run code for a period of time -
tried search , not find im after sorry if has been answered somewhere.
i need run bit of code for period of time, not after period of time. want display random value array fast on page , want keep showing 1 minute , stop.
the code below start after 3 seconds, , not stop , im not sure how can achieve this, appreciated.
var messages = ["good!", "great!", "awesome!", "super!", "nice!"]; function getmessage() { return messages[math.floor(math.random() * messages.length)]; } settimeout(function () { onesecondfunction(); }, 3000); function onesecondfunction() { $('#test').html(getmessage()); settimeout('onesecondfunction()', 100); }
thanks
try
var messages = ["good!", "great!", "awesome!", "super!", "nice!"]; function getmessage() { return messages[math.floor(math.random() * messages.length)]; } var interval = null; settimeout(function() { interval = setinterval(function() { // code here $("#test").html(getmessage()); }, 100); //stop functions after 1 minute. settimeout(function() { clearinterval(interval); }, 60 * 1000); }, 3000);
this create interval after 3 seconds, execute code every 100ms 1 minute.
Comments
Post a Comment