admin管理员组文章数量:1388885
I wrote a function and wanted it to display 1 to 10 after every second in a span. While debugging the program I wanted to log the results to a console and saw different results for firefox and chrome, Chrome also changes the results ater every page refresh.
Below is my function:
function log10() {
for(var i =0;i<=10;i++)
{
console.log(setInterval(function() {
$("span").text(i)
},6000));
}
}
FIREFOX RESULT LOGS: 2 to 11 (via firebug) and remains same on reload.
CHROME SHOWS: 1 to 11 (via built in debugger) And after every reload it shows 22 to 22 / 23 to 33 / 34 to 34 et-al
I am calling function via <body onload = log10();>
Does anyone know whats happening. I am more Interesting in knowing how to log 1 to 10 in span as per my code $("span").text(i)
I wrote a function and wanted it to display 1 to 10 after every second in a span. While debugging the program I wanted to log the results to a console and saw different results for firefox and chrome, Chrome also changes the results ater every page refresh.
Below is my function:
function log10() {
for(var i =0;i<=10;i++)
{
console.log(setInterval(function() {
$("span").text(i)
},6000));
}
}
FIREFOX RESULT LOGS: 2 to 11 (via firebug) and remains same on reload.
CHROME SHOWS: 1 to 11 (via built in debugger) And after every reload it shows 22 to 22 / 23 to 33 / 34 to 34 et-al
I am calling function via <body onload = log10();>
Does anyone know whats happening. I am more Interesting in knowing how to log 1 to 10 in span as per my code $("span").text(i)
-
The
return
fromsetInterval
returnsa JavaScript Object containing all informations needed by an animation (like the this object, the callback function, the length, the frame-rate).
. Src: developer.mozilla/en-US/docs/DOM/window.setInterval – Jasper Commented Oct 24, 2012 at 17:44
3 Answers
Reset to default 3http://jsfiddle/8sdu5/2/
function log10() {
var counter = 1;
var evt = setInterval(function() {
$("span").text(counter++);
if(counter > 10)
clearInterval(evt);
}, 1000);
}
You don't need a loop, the setInterval will "loop" on its own, you just have to have a condition to tell it when to stop.
You are not saving context, using an anonymous function for saving context. You should use setTimeout
instead of setInterval
, as setInterval
will not stop until you stop it (using clearInterval
or by unloading the page). At the other hand setTimeout
will run only once for each loop with delay of 6000ms. jsfiddle
function log10() {
var i = 0;
function repeat(){
if(i > 10 ) return;
setTimeout(repeat, 600);
$("span").text(i);
i++;
}
repeat();
}
You are running into variable binding issues. A different way to solve them is to pass in the number to be shown bined with recursion and setTimeout:
Working jsFidle example
// recursive function
function log10(theNumber) {
if (theNumber <= 10)
{
setTimeout(function() {
$("span").text(theNumber);
log10(++theNumber);
},1000);
}
}
// kick things off
log10(0);
本文标签: javascriptsetInterval() behaves differently in Firefox and ChromeStack Overflow
版权声明:本文标题:javascript - setInterval() behaves differently in Firefox and Chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744627863a2616364.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论