Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
124
rated 0 times [  127] [ 3]  / answers: 1 / hits: 17811  / 13 Years ago, tue, january 24, 2012, 12:00:00

How do I call a jQuery function every 3 seconds?



$(document).ready(function ()
{
//do stuff...

$('post').each(function()
{
//do stuff...
})

//do stuff...
})


I'm trying to run that code for a period of 15 seconds.


More From » jquery

 Answers
25

None of the answers so far take into account that it only wants to happen for 15 seconds and then stop...



$(function() {
var intervalID = setInterval(function() {
// Do whatever in here that happens every 3 seconds
}, 3000);
setTimeout(function() {
clearInterval(intervalID);
}, 18000);
});


This creates an interval (every 3 seconds) that runs whatever code you put in the function. After 15 seconds the interval is destroyed (there is an initial 3 second delay, hence the 18 second overall runtime).


[#87827] Monday, January 23, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alorac

Total Points: 262
Total Questions: 82
Total Answers: 97

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
alorac questions
Sat, Oct 10, 20, 00:00, 4 Years ago
Tue, Sep 22, 20, 00:00, 4 Years ago
Wed, Jul 1, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Sun, May 17, 20, 00:00, 4 Years ago
;