node.js: how to use setInterval and clearInterval?

后端 未结 3 1706
我寻月下人不归
我寻月下人不归 2020-12-16 12:00

This is my JS in nodeJS :

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

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


        
3条回答
  •  隐瞒了意图╮
    2020-12-16 12:48

    You can use "this" in the following example:

    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);

提交回复
热议问题