Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is setTimeOut() function in javascript?

Can i ask what the function of 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);
}
like image 255
dramasea Avatar asked Aug 31 '26 15:08

dramasea


2 Answers

Not sure you what you want.

setTimeout is a method of the global window object. It executes the given function (or evaluates the given string) after the time given as second parameter passed.

Read more about setTimeout.

like image 198
Felix Kling Avatar answered Sep 03 '26 04:09

Felix Kling


setTimeout() just schedules (sets a timer for) a function to execute at a later time, 500ms in this case. In your specific code, it's updating the screen with the current time every half-second (it only schedules one call, 500ms from now...but that startTime call scheduled another).

Also...passing a string to it when you can avoid it is bad practice, for your example it should be:

t = setTimeout(startTime, 500);
like image 39
Nick Craver Avatar answered Sep 03 '26 03:09

Nick Craver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!