Timeouts all the way down
-
JavaScript timeouts, I mean. I'm trying my hand, for the first time ever, to do more than is necessary script. I'm developing a browser based flash-card game, and although I'm building up through independent functions, once I remove redundancy they all become temporally dependent, e.g. once started, the game uses timers to: - Pause before showing the first card in a round - Only show the card for a short time. - Hide the card and show the multiple choice answer card for a medium time - Repeat Each timer triggers the next, in simple code, so it looks like my final code will look like a spiral to hell with nested timeouts. Unless I Really put my mind to it - it could use a break from my perennial nemeses, the little layout/css snafus.
"'Do what thou wilt...' is to bid Stars to shine, Vines to bear grapes, Water to seek its level; man is the only being in Nature that has striven to set himself at odds with himself." —Aleister Crowley
-
JavaScript timeouts, I mean. I'm trying my hand, for the first time ever, to do more than is necessary script. I'm developing a browser based flash-card game, and although I'm building up through independent functions, once I remove redundancy they all become temporally dependent, e.g. once started, the game uses timers to: - Pause before showing the first card in a round - Only show the card for a short time. - Hide the card and show the multiple choice answer card for a medium time - Repeat Each timer triggers the next, in simple code, so it looks like my final code will look like a spiral to hell with nested timeouts. Unless I Really put my mind to it - it could use a break from my perennial nemeses, the little layout/css snafus.
"'Do what thou wilt...' is to bid Stars to shine, Vines to bear grapes, Water to seek its level; man is the only being in Nature that has striven to set himself at odds with himself." —Aleister Crowley
-
Brady Kelly wrote:
Each timer triggers the next
Found the bug. :-D Best Wishes, -David delaune
:-D
Jeremy Falcon
-
JavaScript timeouts, I mean. I'm trying my hand, for the first time ever, to do more than is necessary script. I'm developing a browser based flash-card game, and although I'm building up through independent functions, once I remove redundancy they all become temporally dependent, e.g. once started, the game uses timers to: - Pause before showing the first card in a round - Only show the card for a short time. - Hide the card and show the multiple choice answer card for a medium time - Repeat Each timer triggers the next, in simple code, so it looks like my final code will look like a spiral to hell with nested timeouts. Unless I Really put my mind to it - it could use a break from my perennial nemeses, the little layout/css snafus.
"'Do what thou wilt...' is to bid Stars to shine, Vines to bear grapes, Water to seek its level; man is the only being in Nature that has striven to set himself at odds with himself." —Aleister Crowley
May wanna think about that design man. In years and years of JavaScript I've never had to nest or chain timers like that... ever. Promises or events yeah, but not timers.
Jeremy Falcon
-
JavaScript timeouts, I mean. I'm trying my hand, for the first time ever, to do more than is necessary script. I'm developing a browser based flash-card game, and although I'm building up through independent functions, once I remove redundancy they all become temporally dependent, e.g. once started, the game uses timers to: - Pause before showing the first card in a round - Only show the card for a short time. - Hide the card and show the multiple choice answer card for a medium time - Repeat Each timer triggers the next, in simple code, so it looks like my final code will look like a spiral to hell with nested timeouts. Unless I Really put my mind to it - it could use a break from my perennial nemeses, the little layout/css snafus.
"'Do what thou wilt...' is to bid Stars to shine, Vines to bear grapes, Water to seek its level; man is the only being in Nature that has striven to set himself at odds with himself." —Aleister Crowley
Promises are the way to go. :) Something like this:
// Starting a promise chain:
function wait(timeout) {
return new Promise(function(resolve) {
setTimeout(resolve, timeout);
});
}// Continuing a promise chain
function waitFor(timeout){
return function(result){
return new Promise(function(resolve){
setTimeout(function(){ resolve(result); }, timeout);
});
};
}Usage:
wait(2000)
.then(function() {
alert("Thinking...");
return 42;
})
.then(waitFor(1000))
.then(function(result) {
alert("The answer is " + result)
});
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Promises are the way to go. :) Something like this:
// Starting a promise chain:
function wait(timeout) {
return new Promise(function(resolve) {
setTimeout(resolve, timeout);
});
}// Continuing a promise chain
function waitFor(timeout){
return function(result){
return new Promise(function(resolve){
setTimeout(function(){ resolve(result); }, timeout);
});
};
}Usage:
wait(2000)
.then(function() {
alert("Thinking...");
return 42;
})
.then(waitFor(1000))
.then(function(result) {
alert("The answer is " + result)
});
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I'm the one that up-voted this, but I would like to add a suggestion. If his intent is to do simple animation with this, he's much better off using CSS3 animation with keyframes.
Jeremy Falcon
-
JavaScript timeouts, I mean. I'm trying my hand, for the first time ever, to do more than is necessary script. I'm developing a browser based flash-card game, and although I'm building up through independent functions, once I remove redundancy they all become temporally dependent, e.g. once started, the game uses timers to: - Pause before showing the first card in a round - Only show the card for a short time. - Hide the card and show the multiple choice answer card for a medium time - Repeat Each timer triggers the next, in simple code, so it looks like my final code will look like a spiral to hell with nested timeouts. Unless I Really put my mind to it - it could use a break from my perennial nemeses, the little layout/css snafus.
"'Do what thou wilt...' is to bid Stars to shine, Vines to bear grapes, Water to seek its level; man is the only being in Nature that has striven to set himself at odds with himself." —Aleister Crowley
Also, if your intent is simple animation, delay the showing of a card, etc. you can use CSS3 to pull that off with much less hassle.
Jeremy Falcon
-
Also, if your intent is simple animation, delay the showing of a card, etc. you can use CSS3 to pull that off with much less hassle.
Jeremy Falcon
Near top of the list for iteration 2.
"'Do what thou wilt...' is to bid Stars to shine, Vines to bear grapes, Water to seek its level; man is the only being in Nature that has striven to set himself at odds with himself." —Aleister Crowley
-
I'm the one that up-voted this, but I would like to add a suggestion. If his intent is to do simple animation with this, he's much better off using CSS3 animation with keyframes.
Jeremy Falcon
Thanks, Jeremy. I do plan to do basic animations, like a e.g. rapid fade-in of card as it is flashed on and fade-out when it gets "turned face down" again, but my main concern with timers is game logic. A visibly reducing wait until the next card is shown, then a very short wait while the card is shown, etc. Also the time the player has to choose an answer must be limited, quickest answers tracked, all that spade work.
"'Do what thou wilt...' is to bid Stars to shine, Vines to bear grapes, Water to seek its level; man is the only being in Nature that has striven to set himself at odds with himself." —Aleister Crowley
-
Thanks, Jeremy. I do plan to do basic animations, like a e.g. rapid fade-in of card as it is flashed on and fade-out when it gets "turned face down" again, but my main concern with timers is game logic. A visibly reducing wait until the next card is shown, then a very short wait while the card is shown, etc. Also the time the player has to choose an answer must be limited, quickest answers tracked, all that spade work.
"'Do what thou wilt...' is to bid Stars to shine, Vines to bear grapes, Water to seek its level; man is the only being in Nature that has striven to set himself at odds with himself." —Aleister Crowley
Sounds cool man. I hope it comes out awesome.
Jeremy Falcon