jquery - JavaScript Nested setInterval -
i want write nested timed code setinterval. tried following no response browser (chrome , ff) whatsoever:
setinterval(function() { console.log('1'); setinterval(function(){ console.log('2'); },5000); }, 2500); i expected above code wait 2 seconds , half before starting, , log('1'), wait 5 seconds, , log('2'). happened got no response both browsers (why?)
second point, replaced console.log window.alert. got response time. not desired. first response after 2 seconds , half, second response after 5 seconds, 2 functions start happen simultaneously.
so, want achieve: 2 blocks of code, 2 different time intervals, , no simultaneous occurrence of both blocks.
the behaviour second setinterval attached context of function of first one.
so when function of first setinterval end (is cleared), second existing in context of function of first 1 disappear too
edit
you can use window.setinterval( /*...*/ ) instead of second setinterval make persist behaviour each 2,5 second create interval each 5 second call console.log(2) you'll number of interval growing not you're asking for.
you may want use window.settimeout( /*...*/ ) instead of second setinterval. behaviour following :
1 (2.5sec)
1 (5 sec)
1 (7,5sec)
2 (7,5sec) //1st nested
1 (10sec)
2 (10sec) //2nd nested
...
Comments
Post a Comment