简体   繁体   中英

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/

You may want to take a look at the division. I am suspecting there is a error like this:

10/3 = 3

where all numbers are casted to integer.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM