Actual Combat NODEJS Timer gameplay

Source: Internet
Author: User
Tags setinterval

The Impulse cloud platform itself uses NODEJS as the development language, and the front-end page uses react technology. Timers are usually used when doing projects, and there are two timers in Nodejs: SetTimeout and SetInterval. Relative to the timer settimeout and setinterval How to perform after the specified time is familiar to everyone, the third parameter of the two system functions may be some students are not too clear, I am also in the Pulse Cloud Project found, now learn to sell it now.

The basic usage of these two functions is simple, settimeout is to execute the function body once after the specified time, and setinterval is executed once every time, until the timer is cleared. The application is as follows:

SetTimeout

SetTimeout (function () {

console.log("this is console.log");

},100);

SetTimeout output "This is Console.log" after 100 milliseconds, the timer executes;

SetInterval

Let M = 1;

Let T = setinterval (function () {

console.log(m);m++;if(m>10){    clearInterval(t);}

},100);

The setinterval outputs an M value every 100 milliseconds, and when M is greater than 10, the timer is cleared and no longer outputs.

In fact, the timer can also have a third parameter, or even the fourth nth parameter, of course n is not greater than the function can accept the maximum value of the parameter.

Starting with the third argument, including the third parameter, the callback function of the timer will be passed in in turn as a parameter of the callback function.

SetTimeout

SetTimeout (function (l,m,n) {

console.log(l,m,n);

},100, 1,10,100);

SetTimeout passed the No. 345 parameter respectively, and in the callback function also received three parameters, the final output is

1 10 100

SetInterval

Let M = 1;

Let S = setinterval (function (x, y, z) {

console.log(x,y,z);m++;if(m>10){    clearInterval(s);}

},100,m,m10,m100)

SetInterval as well as settimeout. The callback function is passed in order starting at the third argument.

It is important to note that if the external parameter is a value type, no matter how the No. 345 parameter changes,

The parameters that are received by the callback function are only the values that were passed in for the first time

The Console.log (x, y, z) output above will be

1 10 100

1 10 100

1 10 100

1 10 100

1 10 100

1 10 100

1 10 100

However, if the external parameter is a reference type, such as an obj, then each execution can have different data, for example:

Let M = {a:1};

Let S = setinterval (function (x) {

console.log(x);m.a++;if(m.a>10){    clearInterval(s);}

},100,M)

So, the output of Console.log (x) above would be:

{a:1}

{A:2}

{A:3}

{A:4}

{A:5}

{A:6}

{A:7}

{A:8}

{A:9}

{A:10}

Actual Combat NODEJS Timer gameplay

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.