Search code examples
javascripttiming

accurate timing range with javascript


I'm working on a html5 sound fader widget using the Soundmanager2 library, and I have a function that should be setting a variable to interpolate between 0-100 across 15 seconds, but it takes more than 15 seconds the first time and then less each time after. That inconsistency is driving me nuts.

my js is here: http://wesww.com/nic/peasoup9/js/soundfader.js

CODE: I'm setting a 15 second duration:

function fadeOutSound(soundObject) {
    var fadeLengthInSeconds = 15;

And am doing some math here:

if(soundObject.volume < maximumVolume) {
    var progressRatio = deltaTime / soundObject.fadeLength;
    var actualProgress = progressRatio * maximumVolume;
    soundObject.setVolume( soundObject.volume + actualProgress );

Thanks for any help / tips you might have! If I can add any info/code to make clearer, please let me know

Edit: I ended up going with a self-adjusting timer, such as this: http://www.sitepoint.com/creating-accurate-timers-in-javascript/


Solution

  • All numbers in JavaScript are, by definition, 64-bit floats. The various JavaScript-engines, however, usually type-cast them into simpler number formats if possible (I'm pretty sure at least V8 does this.)

    While I haven't played with it myself, it seem Typed Arrays are currently the best trick to make maths perform on a larger scale. It only works on "modern" browsers, though.