Javascript Can';在meteor应用程序上不使用setTimeout()函数

Javascript Can';在meteor应用程序上不使用setTimeout()函数,javascript,meteor,settimeout,Javascript,Meteor,Settimeout,我不理解这种不同的行为: first.js Template.example.onRendered(function() { timerId = setTimeout ( function() { console.log(Random.id()) }, 3000 ); }); function anything() { console.log(Random.id()); } Template.example.onRendered(function() { timerI

我不理解这种不同的行为:

first.js

Template.example.onRendered(function() {
    timerId = setTimeout ( function() { console.log(Random.id()) }, 3000 );
});
function anything() {
    console.log(Random.id());
}

Template.example.onRendered(function() {
    timerId = setTimeout ( anything(), 3000 );
});
因此,这将在3秒钟后在控制台上给我一个随机id-这正是它应该做的

second.js

Template.example.onRendered(function() {
    timerId = setTimeout ( function() { console.log(Random.id()) }, 3000 );
});
function anything() {
    console.log(Random.id());
}

Template.example.onRendered(function() {
    timerId = setTimeout ( anything(), 3000 );
});

这会立即给我随机id,所以3秒后不会有任何动作。这不是我想要的,但我需要使用一个命名函数,因此第一次尝试对我来说是不够的。

您正在调用您的函数,而不是将其传递给
setTimeout

这样做:

timerId = setTimeout(function() { anything(); }, 3000);  // anonymous function calling the named function

您最初所做的相当于:

timerId = setTimeout(undefined, 3000); // function without return statement will "return" undefined