Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

Commit

Permalink
Fix stop timeout
Browse files Browse the repository at this point in the history
If the ping is manually stopped, it will be restarted after the 30s timeout is fired. This clears the timeout so it won't be fired until it is manually clicked again. It also keeps only one timeout active at any given time, so that you won't have waves of start/stop happening every 30 seconds if you click a bunch of times.
  • Loading branch information
gcochard committed May 22, 2018
1 parent 2301d89 commit 96e23db
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.html
Expand Up @@ -283,16 +283,18 @@ <h2>How does this work?</h2>

let stopped = true;
let ss = document.getElementById('stopstart');
let st;
ss.onclick = function() {
if (stopped) {
console.log('starting');
stopped = false;
ss.children[0].innerText = 'stop';
document.dispatchEvent(new Event('nextping'));
setTimeout(ss.onclick, 30000); // stop after 30s.
st = setTimeout(ss.onclick, 30000); // stop after 30s.
} else {
console.log('stopping');
stopped = true;
clearTimeout(st); // cancel the stop timeout, as it is stopped.
ss.children[0].innerText = 'play_arrow';
}
};
Expand Down

0 comments on commit 96e23db

Please sign in to comment.