Could there be only one timeout in JavaScript?

This code doesn't seem to work ... It shows the twCharCount element only once after a long time. Maybe there can only be one timeout? Any suggestions improving this code? 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("$('#twCharCount').hide()",t); var t =+ step; timer[4] = setTimeout("$('#twCharCount').show()",t); 

OK .. I'm sorry ... I didn't seem to be awake when I wrote this ... of course, im redecalring all the time ... that's why everyone is doing snychronously ...

+4
source share
5 answers
 var intervalId = window.setInterval(function() { $('#twCharCount').toggle(); }, 1000); 

and stop blinking window.clearInterval(intervalId); .

+3
source

Could there be some syntax issues:

 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; must be t += step;

and you do not have to add t again and again.

+2
source

The code is incorrect in many ways :(.
All functions are called at once, because their time (t) is the same.

if you want to increase t , you probably shouldn't declare it on every acces (use var t = ... only once, after which you can access it by name: t = ... ), and you, probably should use += instead of =+ :
a += b is a shortcut for a = a + b , and 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's better to pass a function than a string as the first parameter to the setTimeout function:

 setTimeout(function(){$('#twCharCount').show();},t); 

Sry, but I can’t help myself, this is 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); 
+1
source

There are practically no restrictions on the number of active timeouts.

I do not see any real problems with your code. Probably the problem is not the timeout, but the command that you are executing.

Additional notes (not related to your question, but worth saying):

  • You need the 'var' statement for a variable only once for each function.
  • instead of a timer [...] I would use timer.push () in this case
  • you can use one setInterval () instead
0
source
 var show = false; window.setInterval(function() { if(show) { show = false; $('#twCharCount').show(); } else { show = true; $('#twCharCount').hide(); } }, 1000); 
0
source

Source: https://habr.com/ru/post/1347131/


All Articles