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

I did this a month earlier ... But now its not working ... Code

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

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

I would like to name a specific function every 10 seconds. A warning shows only once. This is a problem in every browser. After this test, I would like to check the URL every 2 seconds and call the AJAX function.

Any help?

+6
source share
4 answers

What setTimeout does (runs once after a certain interval). You are looking for setInterval (repeatedly calling a function with a fixed time delay between each call to this function):

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

Use setInterval instead.

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

Use of setTimeout is possible:

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

but the best solution is setInterval:

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

setTimeout is for a one-time run. Take a look at the setInterval function.

0
source

Source: https://habr.com/ru/post/901003/


All Articles