[英]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. 我正在使用Soundmanager2库处理html5声音渐变器小部件,并且我有一个函数应该将变量设置为在15秒内的0-100之间进行插值,但是第一次要花费15秒以上,然后每次减少时间之后。 That inconsistency is driving me nuts.
这种矛盾使我发疯。
my js is here: http://wesww.com/nic/peasoup9/js/soundfader.js 我的js在这里:
http : //wesww.com/nic/peasoup9/js/soundfader.js
CODE: I'm setting a 15 second duration: 代码:我设置了15秒的持续时间:
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/ 编辑:我最终要使用一个自调整计时器,例如: 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 10/3 = 3
where all numbers are casted to integer. 其中所有数字都强制转换为整数。
All numbers in JavaScript are, by definition, 64-bit floats. 根据定义,JavaScript中的所有数字都是64位浮点数。 The various JavaScript-engines, however, usually type-cast them into simpler number formats if possible (I'm pretty sure at least V8 does this.)
但是,各种JavaScript引擎通常会尽可能将其类型转换为更简单的数字格式(我敢肯定,至少V8会这样做)。
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.
不过,它仅适用于“现代”浏览器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.