'Ready, steady, go + start timer
My countup function worked satisfactorily. But then I wanted to add ready, steady, go. That is, give the user 3 seconds to shift their attention from the button on the screen to the test on their sheets of paper.
So, what I need: ready, steady, go beeps, then a timer starts counting.
Code
var ready_steady_go_counter = 0;
jQuery(function(){
jQuery('#timer-form').on('submit', function(event){
event.preventDefault();
initiate_minutes();
const ReadySteadyGoPromise = new Promise((resolve, reject) => {
function ready_steady_go(){
setTimeout(function () {
if (ready_steady_go_counter > 2) { // Ready (0), steady (1), go (2);
return;
}
beep();
ready_steady_go_counter++;
ready_steady_go(); // <--- this is the "loop"
}, 1000);
}
ready_steady_go();
});
ReadySteadyGoPromise.then(countup, function(){console.log("Alert (timer)!")});
remove_submit_button();
show_planned_times();
});
});
function tick() {
datediffInms = new Date() - startDateTime;
seconds_lapsed = Math.round(datediffInms / 1000);
}
function countup() {
setTimeout(function () { // Breakpoint.
if (stopped) return;
if (!is_paused()) {
tick();
show_time_left();
}
countup(); // <--- this is the "loop"
}, 1000);
}
Problem
Ready, steady, go really beeps as planned. But countup doesn't start. Please, have a look at where a breakpoint is situated (// Breakpoint).
So, the interpreter doesn't stop at the breakpoint. This means that counting up has not started.
Could you help me solve the task by any means: if my code is more or less ok, make adjustments to it, pleas. If my code is absolute garbage, maybe you can help me with your variant.
Solution 1:[1]
The promise you are creating needs to resolve for countup to be called, but you never resolve it.
In this part of your code
if (ready_steady_go_counter > 2) { // Ready (0), steady (1), go (2);
return;
}
replace return with resolve(); return and it should work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BadIdeaException |
