Here is some code that runs a function every second using setTimeout
and then checks that the function wasn't called too early.
var date = Date.now();
var ttl=1000,
to = setTimeout(function loop () {
console.log("triggered!");
if (Date.now() - date < ttl)
console.error("Yes");
date = Date.now();
to = setTimeout(loop, ttl);
}, ttl);
The console.error
call should never occur. But if you are on chrome and hit Alt+Tab
fast enough and enough times it will sometimes appear. I know chrome does some weird stuff with timeouts <1s when a tab looses focus but AFAIK it timeouts are only prolonged, never shortened.
EDIT: While I was writing this post I left such a loop running. When I went back at it here is what I see: