coolaj86 / mediabox

Tools for organizing files.
4 stars 1 forks source link

crossfade is too long when tab is not in focus #10

Open coolaj86 opened 12 years ago

coolaj86 commented 12 years ago

The fader should recalculate on every interval, otherwise it is greatly skewed by setInterval rate-limiting that Chrome implements.

coolaj86 commented 12 years ago

Partially fixed (the volume goes up too sharply)

Initial tests not looking so hot:

var firstTime = Date.now()
  , token
  ;

function checkTimeDiff() {
  var now = Date.now()
    ;

  if (now - firstTime >= 10000) {
    console.log('10 seconds passed');
    clearInterval(token);
    return;
  }

  console.log(now - lastTime);
  lastTime = now;
}

token = setInterval(checkTimeDiff, 4);

Looks like cross-fading isn't possible with tabs using a setTimeout / setInterval methodology

coolaj86 commented 12 years ago

Looks like there may be a workaround:

var firstTime
  , timeWait = 100
  , i
  ;

function checkTime() { 
  console.log(Date.now() - firstTime);
}

function doTimeout() {
  console.log('should have switched tabs by now');
  firstTime = Date.now();
  for (i = 0; i <= 50; i += 1) {
    setTimeout(checkTime, timeWait);
    timeWait += 100;
  }
}

console.log('Switch tabs quick!');
setTimeout(doTimeout, 3000);

Each timeout becomes a whole second, but they can be spaced 100ms apart, effectively creating 100ms timeouts.

There's still the possible initial lag of 1 second... but for a 5 second crossfade it's bareable.