What is the setTimeOut () function in javascript?

Can I ask what is the function of the setTimeOut method in javascript? As below:

function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout('startTime()',500);
}
+3
source share
4 answers

Not sure what you want.

setTimeoutis a method of a global window object. It performs this function (or evaluates the given string) after the time passed as the second parameter passed.

Read more about setTimeout.

+5
source

setTimeout() ( ) , , 500 . ( , 500 ... startTime ).

... , , , :

t = setTimeout(startTime, 500);
+4

startTime (500 ), .

0

setTimeOut . , , startTime , .

Btw. I assume that a delay of 500 ms is used in work around small deviations in hours. You want to update the value of an element every second. To do this, it is better to calculate the time until the next whole second and set this as a delay. This will give you a more accurate watch.

0
source

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


All Articles