Quickly learn how to use setTimeout and setInterval in Node. js, node. jssettimeout

Source: Internet
Author: User
Tags javascript settimeout

Quickly learn how to use setTimeout and setInterval in Node. js, node. jssettimeout

Node. js and js also have timers, time-out timers, interval timers, and timely timers. They and the process. nextTick (callback) function are used to schedule events. Learn how to use setTimeout and setInterval today.

1. setTimeout timeout timer (similar to after in GCD)

You can use the setTimeout (callback, delayMillSeconds, [args]) Method built in node. js. When setTime () is called, The callback function will be after delayMillSeconds

Execution. setTime () returns a timer Object ID. You can send the ID to clearTimeout (timeoutId) before the expiration of delayMillSeconds to cancel the operation.

function myfunc(){ console.log("myfunc");};var mytimeout=setTimeout(myfunc,1000);clearTimeout(mytimeout);
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe timer.jsProcess finished with exit code 0

If clearTimeout (mytimeout) is added, myfunc () is executed ().

"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe timer.jsmyfuncProcess finished with exit code 0

Ii. setInterval interval Timer(Similar to dispatch_source_t or NSTimer in GCD)

The interval timer is used to perform tasks at regular intervals. similar to setTimeout, node. built-in setInterval (callback, delayMilliSecond, [args]) in js to create and return the timer Object Id, which is canceled through clearInterval.

/** * Created by Administrator on 2016/3/11. */function myfunc(Interval){ console.log("myfunc "+Interval);}var myInterval=setInterval(myfunc,1000,"Interval");function stopInterval(){ clearTimeout(myInterval); //myInterval.unref();}setTimeout(stopInterval,5000);

The above Code creates the setInterval callback function myfunc. The parameter is Interval. setInterval is executed every 1 S. setTimeout is executed after 5 seconds. Its callback function cancels the Interval timer.

"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe Interval.jsmyfunc Intervalmyfunc Intervalmyfunc Intervalmyfunc IntervalProcess finished with exit code 0

3. Cancel timer reference from the event Loop

When only timer callback functions exist in the event queue, if you do not want to execute them, you can use setInterval and setTimeout to return the unref () Functions of the object to notify the event loop not to continue.

When unref () and setTimeout are used in combination, an independent timer is used to wake up the event loop. A large amount of use will also affect the performance, and should be used as little as possible.

IV. The execution time of setTimeout and setInterval is inaccurate.

They add the callback to the event queue at a certain interval, and the execution is not too precise.

function simpleTimeout(consoleTime){ console.timeEnd(consoleTime);}console.time("twoSecond");setTimeout(simpleTimeout,2000,"twoSecond");console.time("oneSecond");setTimeout(simpleTimeout,1000,"oneSecond");console.time("fiveSecond");setTimeout(simpleTimeout,5000,"fiveSecond");console.time("50MillSecond");setTimeout(simpleTimeout,50,"50MillSecond");

The output results of the above Code are different after multiple executions.

"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe timer.js50MillSecond: 51msoneSecond: 1000mstwoSecond: 2002msfiveSecond: 5001msProcess finished with exit code 0

The above is all the content in this article. I hope it will be helpful for you to learn how to use setTimeout and setInterval in Node. js.

Articles you may be interested in:
  • SetInterval and setTimeout stop Methods
  • Precautions for using setInterval and setTimeout in jQuery
  • SetInterval and setTimeout methods in Jquery
  • When setTimeout () and setInterval () are called and executed in Js
  • Jump effect in 5 seconds (setInterval/SetTimeOut)
  • Differences between javascript setTimeout and setInterval timing
  • Deep understanding of setTimeout and setInterval
  • How to use JavaScript SetInterval and setTimeout
  • Difference between setTimeout () and setInterval () Methods
  • SetInterval and setTimeout instances in js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.