admin管理员组文章数量:1391943
For a reason that I cannot fathom, the following function doesn't seem to work.
function timerTick()
{
var t=setTimeout(timerTick,1000);
}
Everything should be working, but when I call the function, the console simply says 'undefined'.
Ideas?
For a reason that I cannot fathom, the following function doesn't seem to work.
function timerTick()
{
var t=setTimeout(timerTick,1000);
}
Everything should be working, but when I call the function, the console simply says 'undefined'.
Ideas?
Share Improve this question asked Aug 19, 2011 at 11:55 help mehelp me 91 silver badge2 bronze badges 6-
1
Why are you assigning the value to a
var
that never gets referenced again? Do you mean to return the result ofsetTimeout
instead? – Andrzej Doyle Commented Aug 19, 2011 at 11:57 - The console output must e from somewhere else, the function is perfectly valid. – David Hellsing Commented Aug 19, 2011 at 11:58
-
Add
alert('i am called');
inside timerTick(). If you are calling it from the console then 'undefined' is correct behaviour, since the function call produces no return object. – arunkumar Commented Aug 19, 2011 at 11:59 -
setTimeout
returns a timeout ID, which can be used to clear the timeout. Are you trying to find out how many milliseconds thesetTimeout
has left? – Alex Commented Aug 19, 2011 at 12:00 - I'm trying to make an recursively looping function. I added the alert - it pops up once, then the console says 'undefined' and it never shows again. – help me Commented Aug 19, 2011 at 12:01
2 Answers
Reset to default 5Everything IS working. What you're seeing is the return value of the invokation of timerTick
itself which, as it stands, does not have a return statement and whose return value will thus be undefined
. (The local variable t
is not returned automatically!)
If you add a
console.log( "It's me. Again!" );
inside timerTick
and call it you'll be seeing it every second in the console as expected.
EDIT: Typo in code and clarification: The return value of functions not invoked from the console, such as through setTimeout
or setInterval
, will not be printed to the console.
Everything should be working, but when I call the function, the console simply says 'undefined'.
Ideas?
May be your past this code into console and press enter (for example, in Chrome)? In this case console says 'undefined'. If your need to run function timerTick try next: 1)
function timerTick()
{
console.info('i am called');
var t=setTimeout(timerTick,1000);
}
timerTick();
or 2)
(function timerTick()
{
console.info('i am called');
var t=setTimeout(timerTick,1000);
})();
本文标签: timerJavascriptsetTimeout undefinedStack Overflow
版权声明:本文标题:timer - Javascript - setTimeout undefined? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744737229a2622404.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论