我做了一个关于Meteor.setTimeout()使用的示例Meteor.在这个例子中我得到一个错误.我对此没有任何想法.所以请看下面的代码,错误并建议我怎么办?
错误:
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
Run Code Online (Sandbox Code Playgroud)
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
});
}
Run Code Online (Sandbox Code Playgroud)
Tob*_*old 20
问题是setTimeout期望一个函数作为第一个参数,但是你传递的评估Test("10")结果是"未定义".
您可以通过将您的调用包装Test1在匿名函数中来解决此问题:
Meteor.setTimeout(function(){Test("10");}, 1000);
Run Code Online (Sandbox Code Playgroud)