Skip to content

Instantly share code, notes, and snippets.

@soarez
Created July 21, 2015 22:04
Show Gist options
  • Save soarez/be48304a644db4611b91 to your computer and use it in GitHub Desktop.
Save soarez/be48304a644db4611b91 to your computer and use it in GitHub Desktop.
console.log('experiment')
var intervalTime = 1000;
setInterval(runSomething, intervalTime);
var lastTime = Date.now();;
function runSomething() {
var now = Date.now();
var elapsed = now - lastTime;
lastTime = now;
var lag = elapsed - intervalTime;
console.log('Running something. Lag:', lag);
}
var count = 2000;
var limit = 50;
var scheduled, done;
proceed();
function proceed() {
scheduled = 0;
done = 0;
while (count --> 0 && ++scheduled <= limit)
setTimeout(somethingElse(count), 500);
}
function somethingElse(count) {
return function() {
if (count % 100 === 0)
console.log(2000 - count);
var start = Date.now();
while (Date.now() - start < 4);
done++;
if (done === limit)
proceed();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment