简体   繁体   English

setInterval,setTimeout和Node.js中的回调函数

[英]setInterval, setTimeout with callback function in Node.js

I write an application in Node.js which are going to be engine like as simulator game fg. 我在Node.js中编写了一个应用程序,它将像模拟器游戏fg一样成为引擎。 football match. 足球比赛。 I want to have "on-line broadcast" and for this reason I created functions like below. 我想进行“在线广播”,因此我创建了如下功能。 I wonder why my code doesn't work properly. 我不知道为什么我的代码无法正常工作。 What I'm expecting: 我期望的是:

wait 等待

wait 等待

wait 等待

wait 等待

wait 等待

something 某事

wait 等待

wait 等待

wait 等待

wait 等待

wait 等待

something 某事

wait 等待

etc. 等等

but I don't have any idea what's wrong with my code, look at this: 但我不知道我的代码有什么问题,请看一下:

getText: function() {
    console.log("something");
},
setDelay: function(callback) {
    var that = this;
    setTimeout(function() {
        callback();
    },5000);
},
play: function() {
    var that = this;
    setInterval(function(){
        console.log("wait");
        that.setDelay(that.getText);    
    },1000);
},

I'm getting something like this: 我得到这样的东西:

wait 等待

wait 等待

wait 等待

wait 等待

wait 等待

something 某事

wait 等待

something 某事

wait 等待

something 某事

etc. 等等

any ideas will be appreciated. 任何想法将不胜感激。

It's because you're calling setDelay() every second: 这是因为您每秒都要调用setDelay()

getText: function() {
    console.log("something");
},
setDelay: function(callback) {
    var that = this;
    setTimeout(function () {
        callback();
    }, 5000);
},
play: function() {
    var that = this;
    setInterval(function () {
        console.log("wait");
        that.setDelay(that.getText); // << here
    }, 1000);
},

What happens: 怎么了:

1s -> setDelay() -> timeout1 is created 1s-> setDelay()-> timeout1已创建
2s -> setDelay() -> timeout2 is created 2s-> setDelay()-> timeout2已创建
3s -> setDelay() -> timeout3 is created 3s-> setDelay()-> timeout3已创建
4s -> setDelay() -> timeout4 is created 创建4s-> setDelay()-> timeout4
5s -> setDelay() -> timeout5 is created 5s-> setDelay()-> timeout5已创建
6s -> setDelay() -> timeout6 is created // timeout1's 5s are done, so it fires 6s-> setDelay()-> timeout6已创建// // timeout1的5s完成,因此触发
7s -> setDelay() -> timeout7 is created // timeout2's 5s are done, so it fires 7s-> setDelay()-> timeout7被创建// timeout2的5s完成,因此触发
8s -> setDelay() -> timeout8 is created // timeout3's 5s are done, so it fires 8s-> setDelay()-> timeout8被创建// timeout3的5s完成,因此触发
and so on... 等等...

Inside of your play function, every second, you're running that.setDelay , which then waits five seconds before running console.log("something"); play功能内部,每秒运行that.setDelay ,然后等待五秒钟,然后再运行console.log("something"); that.setDelay .

What you could do instead is have two setInterval 's running: 您可以做的是运行两个setInterval

play: function() {
    var that = this;
    setInterval(function () {
        console.log("wait");
    }, 1000);
    setInterval(function () {
        that.getText();
    }, 5000);
},

This way, every second you'll get the output "wait", and every five seconds, the output "something". 这样,您将每秒获得输出“ wait”,每隔五秒钟便获得输出“ something”。

Merry Christmas! 圣诞节快乐!

I would try to unpick the two intermingled timeouts/intervals and have just one that you clear/reset once you've reached the loop limit. 我将尝试取消选择两个混合的超时/间隔,并在达到循环限制后仅清除/重置一个。 It makes it a little easier to follow. 这使得它更容易遵循。 Something like this: 像这样:

function play(count) {
  var count = count || 1;
  var t;
  console.log(count);
  if (count !== 0 && count % 5 === 0) {
    clearTimeout(t);
    console.log('something');
    t = setTimeout(play, 5000, 1);
  } else {
    t = setTimeout(play, 1000, ++count);
  }
}

play();

DEMO 演示

Or, in line with your own code: 或者,根据您自己的代码:

var obj = {
  delay: 5000,
  timer: 1000,
  getText: function () {
    console.log('something');
  },
  play: function(count) {
    var count = count || 1;
    var t;
    if (count !== 0 && count % 5 === 0) {
      clearTimeout(t);
      this.getText();
      t = setTimeout(this.play.bind(this), this.delay, 1);
    } else {
      t = setTimeout(this.play.bind(this), this.timer, ++count);
    }
  }
}

obj.play();

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM