Sunil Kumar
Sunil Kumar

Reputation: 632

Javascript setTimeout function in window load event... not working

I've done this a month before... But now its not working... The code is

window.onload = function(){
 setTimeout(function(){
   alert("Hello");
 }, 10000);
};

This is written in script in head of the test.php page. The script and other tags are correct.

I would like to call a specific function every 10 seconds. The alert just shows once only. This is problem in every browser.... After this testing i would like to check the url every 2 seconds and call an AJAX function.

Any Help??

Upvotes: 9

Views: 57875

Answers (4)

abuduba
abuduba

Reputation: 5042

var fn = function(){alert("Hello")};

It is possible using setTimeout:

window.onload =  function(){ setTimeout( function(){ fn();window.onload() },10000) };

but the best solution is setInterval:

window.onload = function() { setInterval(fn,10000)};

Upvotes: 3

MOleYArd
MOleYArd

Reputation: 1268

setTimeout is intended for one-time run. Look at setInterval function.

Upvotes: 0

James Allardice
James Allardice

Reputation: 165941

That's what setTimeout does (executes once after a specified interval). You're looking for setInterval (calls a function repeatedly, with a fixed time delay between each call to that function):

window.onload = function(){
   setInterval(function(){
       alert("Hello");
   }, 10000);
};

Upvotes: 14

Quentin
Quentin

Reputation: 943157

Use setInterval instead.

Upvotes: 3

Related Questions