Joined February 2013
·
B7b49831b9a1e3f260d412488bbda9f8

Abhishek Kandoi

An Inquisitive Programmer who loves Music and Dance. at SDSLabs
An Inquisitive Programmer who loves Music and Dance.
SDSLabs
·
Delhi, India
·
·
·

Let us consider the two calls of the function onresize corresponding to the two event triggers.

The first time the function is called, window.clearTimeout(timeoutId) does nothing as timeoutId is not yet initialized. Now, a call to function doSomething will be made after a time interval of 500 milliseconds (notice the function is yet to be called). And the assignment

timeoutId = window.setTimeout(doSomething, 500);

will store a numerical id corresponding to the timeout for later use.

The second time the function is called, the previously declared timeout is cleared and since this happens within a time-gap of 500 milliseconds(which is a reasonable assumption given the speed of modern JavaScript engines) the function was never called. But after the statement:

timeoutId = window.setTimeout(doSomething, 500);

another timeout is defined and the function doSomething is called after 500 milliseconds.

So effectively the function doSomething is called only once.
Hope this makes the functioning clear.

Posted to A better git log over 1 year ago

Neat and simple...:)

Posted to Little Known Terminal Command over 1 year ago

Thanks. :)

Achievements
217 Karma
8,689 Total ProTip Views