node.js:如何使用 setInterval 和 clearInterval?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46588994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:31:24  来源:igfitidea点击:

node.js: how to use setInterval and clearInterval?

node.js

提问by yarek

This is my JS in nodeJS :

这是我在 nodeJS 中的 JS :

function test(a, room, seconds) {
    console.log(a, room, seconds);
}

intervalid = setInterval(test, 1000, 'room', 20);
console.log('intervalid', intervalid);

Which returns me the output:

它返回我的输出:

intervalid Timeout {
    _called: false,
    _idleTimeout: 1000,
    _idlePrev: TimersList {
        _idleNext: [Circular],
        _idlePrev: [Circular],
        _timer: Timer { '0': [Function: listOnTimeout], _list: [Circular] },
        _unrefed: false,
        msecs: 1000,
        nextTick: false
    },
    _idleNext: TimersList {
        _idleNext: [Circular],
        _idlePrev: [Circular],
        _timer: Timer { '0': [Function: listOnTimeout], _list: [Circular] },
        _unrefed: false,
        msecs: 1000,
        nextTick: false
    },
    _idleStart: 377,
    _onTimeout: [Function: test],
    _timerArgs: [ 'room', 20 ],
    _repeat: 1000
}

Whereas in simple Javascript it returns a simple INTEGER number

而在简单的 Javascript 中,它返回一个简单的整数

When I attach interval to an existing user object, example :

当我将时间间隔附加到现有用户对象时,例如:

user.intervalid = setInterval(test, 1000, 'room', 20);

I am not able to clearInterval any more :

我不能再 clearInterval 了:

clearInterval(user.intervalid); // does not work

回答by Syed Ayesha Bebe

Using setInterval()

使用 setInterval()

What if you need to repeat the execution of your code block at specified intervals? For this, Node has methods called setInterval() and clearInterval(). The setInterval() function is very much like setTimeout(), using the same parameters such as the callback function, delay, and any optional arguments for passing to the callback function.

如果您需要以指定的时间间隔重复执行代码块怎么办?为此,Node 具有称为 setInterval() 和 clearInterval() 的方法。setInterval() 函数与 setTimeout() 非常相似,使用相同的参数,例如回调函数、延迟以及传递给回调函数的任何可选参数。

A simple example of setInterval() appears below:

下面是一个 setInterval() 的简单示例:

var interval = setInterval(function(str1, str2) {
  console.log(str1 + " " + str2);
}, 1000, "Hello.", "How are you?");

clearInterval(interval);

This is another way when you want to keep only one interval running every minute

当您只想保持每分钟运行一个间隔时,这是另一种方式

function intervalFunc() {
    console.log("Hello!!!!");
     }
    setInterval(intervalFunc,1500);

In the above example, intervalFunc() will execute about every 1500 milliseconds, or 1.5 seconds, until it is stopped . Hope this helps.

在上面的例子中, intervalFunc() 大约每 1500 毫秒或 1.5 秒执行一次,直到它停止。希望这可以帮助。

回答by farshad-nsh

You can use "this"in the following example:

您可以在以下示例中使用“this”

const dgram = require('dgram');
const message = Buffer.from('from raspberry pi 3');
const client = dgram.createSocket('udp4');
var count = 0;

function intervalFunc() {
  count++;
  client.send(message, 3001, 'localhost', (err) => {
    //client.close();
  });
  if (count == '5') {
    clearInterval(this);
  }
}
setInterval(intervalFunc, 1500);

回答by FTWinston

This just caught me. Exactly the same scenario, clearInterval simply wasn't working.

这只是抓住了我。完全相同的场景, clearInterval 根本不起作用。

Looking in the console, the result of setInterval was an object, not a number. I was quite confused.

在控制台中查看, setInterval 的结果是一个对象,而不是一个数字。我很困惑。

The solution was to remove this import statement, which had snuck in:

解决方案是删除此导入语句,该语句已潜入:

import { setInterval } from 'timers';

import { setInterval } from 'timers';