
Originally Posted by
darco9x2
i found a code for background sound, using random sounds. but i dont understand.
The code is badly written. A better approach would be:
Code:
var sound = [
'...',
'...',
'...',
'...'
],
i = Math.floor((Math.random() % 1) * sound.length);
I think the code is self-explanatory: an array of URLs is created and assigned to the sound variable. Next, a random number in the range [0,1) is created and multiplied by the number of elements in the array. The resulting number, in the range [0,n), is then truncated to an integer; [0,n-1]. This final value will then index an element in the array: sound[i]. This approach is extensible as you could have as many, or as few, URLs as you like in the sound array without making changes to the code.
I can't say I'm impressed by the browser detection, but as I don't know the current support available, I can't suggest an alternative. You could probably use the object element as a catch-all, though it would only be supported by more modern user agents.
Mike
Bookmarks