Can there only be one timeout in JavaScript?
This code seems not to work... It shows the twCharCount element only once after a long time. Could it be that there can only be one timeout set? Any suggestions making this code better? Thanks for any advice...
var timer = new Array();
var t=0;
var step=1000;
counter.hide();
var t =+ step;
timer[0] = setTimeout("$('#twCharCount').show()",t);
var t =+ step;
timer[1] = setTimeout("$('#twCharCount').hide()",t);
var t =+ step;
timer[2] = setTimeout("$('#twCharCount').show()",t);
var t =+ step;
timer[3] = setTimeout("$('#tw开发者_运维知识库CharCount').hide()",t);
var t =+ step;
timer[4] = setTimeout("$('#twCharCount').show()",t);
ok .. i'm sorry ... i kind of was not verry awake while writing this ... of course im redecalring all the time ... this is why the all execute snychronously ...
var intervalId = window.setInterval(function() {
$('#twCharCount').toggle();
}, 1000);
and to stop blinking window.clearInterval(intervalId);
.
Could it just be some syntax problems:
var timer = [];
var t=0;
var step=1000;
counter.hide();
t += step;
timer[0] = setTimeout("$('#twCharCount').show()", t);
t += step;
timer[1] = setTimeout("$('#twCharCount').hide()", t);
t += step;
timer[2] = setTimeout("$('#twCharCount').show()", t);
t += step;
timer[3] = setTimeout("$('#twCharCount').hide()", t);
t += step;
timer[4] = setTimeout("$('#twCharCount').show()", t);
t =+step;
should be t += step;
and you shouldn't redeclare t over and over again.
The code is wrong in many ways :(.
Your functions are all being called in the same time because their time (t) is the same.
if you want to increment t
, you should probably not declare it on every acces (use var t = ...
only once; after that you can access it by it's name : t = ...
) and you should probably use +=
instead of =+
:
a += b
is a shortcut to a = a + b
, while a =+ b
is a shortcut to a = parseInt(b)
.
You probably wanted to write:
var timer = [];
var t=0;
var step=1000;
counter.hide();
t += step;
timer[0] = setTimeout("$('#twCharCount').show()", t);
t += step;
timer[1] = setTimeout("$('#twCharCount').hide()", t);
t += step;
timer[2] = setTimeout("$('#twCharCount').show()", t);
t += step;
timer[3] = setTimeout("$('#twCharCount').hide()", t);
t += step;
timer[4] = setTimeout("$('#twCharCount').show()", t);
One more thing, it is better to pass a function than a string as the first parameter for the setTimeout
function:
setTimeout(function(){$('#twCharCount').show();},t);
Sry, but i can't help my self, this is the optimized code for you :
var timer = [],
step = 1000,
n = 4,
el = $('#twChartCount');
for(var i=0;i<n;i++)
if(i%2)
timer[i] = setTimeout(function(){el.hide();},i*step);
else
timer[i] = setTimeout(function(){el.show()},i*step);
There is virtually no limit on the number of active timeouts.
I don't see any real problems with your code. Probably the problem is not the timeout but the command you're executing.
Additional notes (not related to your question, but worth to say):
- You need the 'var' statement for a variable only once per function.
- instead of timer[...] I'd use timer.push() in this case
- you can use a single setInterval() instead
var show = false;
window.setInterval(function() {
if(show)
{
show = false;
$('#twCharCount').show();
}
else
{
show = true;
$('#twCharCount').hide();
}
}, 1000);
精彩评论