javascript setTimeout 不适用于 node.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29833232/
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-10-28 11:08:30  来源:igfitidea点击:

setTimeout not working with node.js

javascriptnode.jssettimeout

提问by user3773712

I am writing mocha test cases to test the following steps. I intend to make an API call and wait for 30 minutes before calling another API. I am using an internal node API which was written to call REST APIs to write this test case. But for some reason, setTimeoutis not waiting for the given ms. Can someone please help me?

我正在编写 mocha 测试用例来测试以下步骤。我打算进行 API 调用并等待 30 分钟,然后再调用另一个 API。我正在使用一个内部节点 API,它被编写来调用 REST API 来编写这个测试用例。但由于某种原因,setTimeout不是等待给定的毫秒。有人可以帮帮我吗?

 describe('Checkout - ', function() {
   before(function() {
     lapetus = test.Lapetus;
   });
  it('Get purchase contract after session is expired [C123]',     function(done) {
    this.timeout(180000000);
    lapetus.run(function() {
     // create customer
     ......

     // create new cart and add one item
     ......

    // create new contract with empty cart id
    .......
    var pc_id =....;

    // wait for 30 minutes for the session to expire
    console.log('wait... ' + new Date);
    this.setTimeout(getPC(lapetus,pc_id), 18000000);
    console.log('ok... ' + new Date);
    done();
  });
});

  var getPC = function(lapetus, pc_id){
   // get newly created purchase contract and verify session expired message throws
    .....
    ......
 };
  });

It does not wait 30 minutes. The call back I put in (the getPCmethod) executes immediately.

它不会等待 30 分钟。我输入的回调(getPC方法)立即执行。

Any help is appreciated.

任何帮助表示赞赏。

Thanks

谢谢

回答by Chris

Your call back is executing immediately because you're calling it then and there.

您的回调会立即执行,因为您当时和那里都在调用它。

Change it to this.setTimeout(function() { getPC(lapetus,pc_id); }, 18000000);so that what you want to execute is in a function for setTimeoutto call.

将其更改为,this.setTimeout(function() { getPC(lapetus,pc_id); }, 18000000);以便您要执行的内容在setTimeout要调用的函数中。

** Edit**

**编辑**

In relation to my last comment. You should move your "ok..." inside of the function you've put inside of setTimeout. This will cause the "ok..." to execute right before getPCis called.

关于我最后的评论。你应该把你的“ok...”移到你放在里面的函数里面setTimeout。这将导致“ok...”在getPC被调用之前执行。

this.setTimeout(function() {
    console.log('ok... ' + new Date);
    getPC(lapetus,pc_id)
}, 18000000);

It is important to understand that setTimeoutwill only start a timer which will execute your code later. setTimeoutis going to start that timer, and not wait for it to finish. It will move to the next bit of code once it is done starting that timer.

重要的是要了解它setTimeout只会启动一个计时器,稍后将执行您的代码。setTimeout将启动该计时器,而不是等待它完成。一旦完成启动该计时器,它将移动到下一位代码。