javascript 流星计时器中的 Meteor.setTimeout() 问题?

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

Meteor.setTimeout() issue in Meteor Timers?

javascriptmeteor

提问by user3213821

I did a sample example on Meteor.setTimeout()using Meteor. In this example i get an error. I didn't have any idea about this.So please see the below code,error and suggest me how to do?

我做了一个Meteor.setTimeout()使用Meteor. 在这个例子中,我得到一个错误。我对此一无所知。所以请查看下面的代码,错误并建议我怎么做?

Error :

错误 :

Exception in setTimeout callback: TypeError: undefined is not a function
    at _.extend.withValue (http://localhost:3000/packages/meteor.js?8ec262df25783897eaad01255bc8bd1ca4e78b24:773:17)
    at http://localhost:3000/packages/meteor.js?8ec262df25783897eaad01255bc8bd1ca4e78b24:358:45
    at http://localhost:3000/packages/meteor.js?8ec262df25783897eaad01255bc8bd1ca4e78b24:801:22 

JS Code :

JS代码:

   if (Meteor.isClient) 
{
  Meteor.setTimeout(Test("10"), 1000);
  Meteor.setInterval(Test1, 1000);

  Template.hello.greeting = function () 
  {
    return "Welcome to timerapp.";
  };

  Template.hello.events
  ({
    'click input' : function () 
    {
      // template data, if any, is available in 'this'
      if (typeof console !== 'undefined')
        console.log("You pressed the button");

        //Test();
    }
  });
}
function Test(x)
{
   console.log("*** Test() ***"+x);
}
function Test1()
{
   console.log("*** Test1() ***");
}
if (Meteor.isServer)
 {

  Meteor.startup(function ()
  {
    // code to run on server at startup
  });


}

回答by Tobold

The problem is that setTimeoutexpects a function as a first parameter but you are passing the result of evaluating Test("10")which is "undefined".

问题是setTimeout期望函数作为第一个参数,但您传递的Test("10")是“未定义”的评估结果。

You can solve the issue by wrapping your call to Test1in an anonymous function:

您可以通过将您的调用包装Test1在匿名函数中来解决该问题:

Meteor.setTimeout(function(){Test("10");}, 1000);