etsted Posted April 4, 2014 Share Posted April 4, 2014 when i use the setTimeout function why do i have to use quotes around the first parameter when it is a function? <script> function renderTime() {var time = new Date();var h = time.getHours();var m = time.getMinutes();var s = time.getSeconds();var diem = "AM"; if( h == 0){ h = 12;} else if(h > 12){ h = h - 12;diem = "PM";} if(h < 10){h = "0" + h;} if(m < 10){m = "0" + m;} if(s < 10){s = "0" + s;} var clock = document.getElementById("displayClock");clock.innerText = h + ":" + m + ":" + s + " " + diem;clock.innerText = h + ":" + m + ":" + s + " " + diem;setTimeout(renderTime(),1000); }renderTime();</script> Link to comment Share on other sites More sharing options...
davej Posted April 4, 2014 Share Posted April 4, 2014 If there are no parameters then leave the parens off. If there are parameters then enclose it in an anonymous function. setTimeout(renderTime,1000);setTimeout(function(){renderTime(1000)},1000); Link to comment Share on other sites More sharing options...
thescientist Posted April 4, 2014 Share Posted April 4, 2014 because in your example you are calling the function immediately, with the ()'s and thus you are passing the return value of that function to setTimeout. It is preferable to just pass a reference to the function, as in DaveJ's examples. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now