setInterval...">

Javascript interval

How to use spacing in js? For example, I want to call a function every 5 seconds?

<script type="text/javascript"> setInterval(openAPage(), 5000); function openAPage() { var startTime = new Date().getTime(); var myWin = window.open("http://www.sabah.com.tr","_blank") var endTime = new Date().getTime(); var timeTaken = endTime-startTime; </script> 

This script does not work, does anyone know why?

+4
source share
3 answers

These answers are complete and good; I just want to specifically fix yours. See Other Answers for HOW / WHY.

 setInterval(openAPage, 5000); 

Note the absence () .

In addition, you are missing the closing } openAPage () function.

+3
source
 setInterval(functionName, 5000) 
+3
source
 setInterval(function(){ /* your code here */ }, 5000); 

And if you need to pass data to a function, you can do it with a close:

 setInterval(function(param){ return function(){ console.log(param); }; }("hello"), 5000); 

prints "hello" on the console.

+2
source

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


All Articles