Reputation: 944
I have two functions in Javascript code one looped in the other.
Something like:
function doThis(){
doThat();
window.setTimeout( doThis , 2000);
}
function doThat(){
if(someCondition){
window.clearTimeout(id)
}
var id = window.setTimeout( doThat, 10000);
}
What exactly I want to do:
doThis
is called as soon as the window
is loaded. Now, as soon as it is called, it should hand over the control to doThat()
which waits for 10 seconds before executing again.
Now, doThat()
is executed till the if
codition is satisfied. And then, it should hand the control back to doThis()
which should then wait for 2 seconds and repeat this cycle over and over.
I do not want to use jQuery(callback or chaining stuff) or any other javaScript libarary.How to do this?
Upvotes: 0
Views: 108
Reputation: 215039
If I understood correctly,
function first() {
setTimeout(second, 1000)
}
function second() {
if(condition) {
setTimeout(first, 2000)
} else {
setTimeout(second, 10000)
}
}
No need to save the timeout anywhere or to clear it.
Upvotes: 2