Download as pdf
Download as pdf
You are on page 1of 6
oe Node 3S 227 2. Cancelling Timers: It is used to cancel the scheduled timer « clearlmmediate(): It is used to stop an immediateObject, as created by ~ setimmediate Clearinterval(): It is used to stop an intervalObject, as created by setinterval, _e clearTimeout(): It prevents a timeoutObject, as created by setTimeout 2.7.1 Scheduling Timers 1. setTimeout() setTimeout() method is used to execute a piece of code after a specified time. . © The setTimeout() function takes three arguments: ¢ First: the function to be run (user defined function) + Second: the delay in milliseconds (time parameter to delay execution) « Third (optional): an argument to be passed to the function The time parameter, which is optional to pass, stores ‘the time in milliseconds (1 second = 1000 milliseconds). The code will . executed after the time that is passed as the argument. . Syntax: ea let timeoutld = setTimeout(funcl, delay, argument!, argument2, ...]);// syntax let timeoutld = setTimeout(func{, delay}); // option 2 let timeoutld = setTimeout(code[, delay]); // option 3 2.28 Fuh Hache Waly ti Outputs According 10 the above code, the console,log() written in the callback f Will exeoute after 10000 milliseconds, 1.6 10 seconds, 2 setlaterval()s + The setinterval method is similar to the set Timeout) function in + — Hrepoats the specified finetion after a time interval, + A funetion is executed repeatedly after a certain amount of time by the user in this function, * Wrequires three arguments: * First: the function to be executed * Second: the delay in milliseconds * — Third (optional); an argument to be passed to the function Syntax: Jet intervalld = setInterval(callbackFunetion, [delay,, /loption 1 Jet intervalld = setInterval(callbackFunction{, delayDuration)); let intervalld = setInterval(code, [delayDuration]); /option 3 // option 2 setinterval(printSomeStuff, 5000) function print! console.log(*This message is displayed from setini } uf) { Herval method’) Output: ‘The below code will keep on printing the Matement present in console.log() very 5000 milliseconds i.c 5 seconds, ie displayed fr Int pers) is displayed fr petInter method 10 displayed from setty method » displayed from setInts method brine batt Tren aT) vr aes ST} POON TT tio) Node JS 529 3. setimmediate(): « _ setimmediate() method is used to execute code at the end of the loop cycle. e _ setimmediate() method queues its callback on the event loop and any function that is passed to setimmediate method() is executed in the next iteration of the event loop. « _ setimmediate() is similar to setTimeout() with a Oms time delay. « setimmediate() takes two arguments: First: The first argument passed to the setImmediate() method is a function that will be executed. ¢ Second (optional): the function’s parameter Synatx: let immediateld = setmmediate(callbackFunction, [param], param2, ...}); Jet immediateld = setImmediate(callbackFunction); ial Example: setImmediate(printSomething) function printSomething(){ console.log(‘This message is displayed immediately by setlmmediate()”) } Output: The above console.log() is printed as soon as the code is executed. Boil hea 2.1.2 Cancelling Timers 1, ¢learTimeout(): * The clearInterval() function halts the interval object created by the SetInterval() function: y * i, The clearTimeout() method allows to destroy/ cance! the timer created by the set Timeout object. * The seTimeout() methods returns anid (number) which is stored ina variable and that variable is passed to clearTimeout() method which clears the timer, 2,30 Full Stack Web Develop, ' * — The object returned when the timeout is scheduled, it exports the timeout and timeout.unref() functions that help us control the behavior. ° The timeout.hasRef() method, it returns true; the timeout object keeps the | Event Loop active, : * The timeout.ref() method returns the reference to timeout and Fequests no, Not to exit the Event Loop as long as the timeout object is active an, timeout.unref{) method on being called will not require the Node} Loop to remain active. * The timeout.refresh() method allows refreshing a timer without alloca new Javascript object, Example: . var timeoutObject; timeoutObject = setTimeout(callBack, 10000) function callBack(){ console.log(“This message is from setTimeout method”) } function stopTimeout(){ clearTimeout(timeoutObject) 3 Output: Mush Mr tote y be ede hen Meee et SEES | 2. clearInterval(); ° The clearInterval() method is us ed to cancel the object create: setlnterval method(), * — The id which is associated with th eat e setInterval() method is used to !<" timer set by the method, Node 3S 2.31 Example: var intervalObject; intervalObject = setinterval(printSomeStuff, 5000) function printSomeStuff(){ console.log(*This message is displayed from setInterval method’) } function stopIntervalObject(){ clearInterval(intervalObject) } Output: This message is displayed from setInterval method This message is displayed from setInterval method Beebe eure Sei} displayed from setInterval method This message is displayed from setInterval method This message is displayed from setInterval method message is-displayed from setinterval method 3. clearImmediate(): The clearlmmediate() object is used in node.js to clear the timer set by SetImmediate() method. The clearlmmediate() method is used to cancel /destroy the object which is created by the setImmediate() method. The object returned when the immediate is scheduled, it exports the immediate.ref() and immediate.unref() functions that help us control the behavior. The immediate.hasRef{) method, it returns true; the immediate object keeps the Event Loop active. The immediate.ref() method returns the reference to immediate and requests node not to exit the Event Loop as long as the immediate object is active and the immediate.unref{) method on being called will not require the Nodejs Event Loop to remain active,

You might also like