JavaScript - Try/Catch within setInterval()
Posted: Sun Aug 12, 2012 9:18 pm
I don't know much about JavaScript, but now I need to add a small bug fix to a code I'm using on my website.
In Internet Explorer 9 (and probably Chrome/Safari) setting audio.currentTime too early will cause a "Dom Exception INVALID_STATE_ERR" because the audio file hasn't been completely loaded yet. Example:For some strange reason it works perfectly fine in Firefox...
Anyway, what I'm now trying to implement gets basically explained in the first post over here:
What I've set up so far is the following replacement:What do you think? Will this work correctly or do you spot any errors I overlooked?
In Internet Explorer 9 (and probably Chrome/Safari) setting audio.currentTime too early will cause a "Dom Exception INVALID_STATE_ERR" because the audio file hasn't been completely loaded yet. Example:
Code: Select all
audioplayer.load() // Preload
$(audioplayer).on('loadeddata', function() { // jQuery: When audio fully loads
audioplayer.currentTime = 123 // <-- ERROR: Cannot be changed yet!
audioplayer.play()
})Anyway, what I'm now trying to implement gets basically explained in the first post over here:
So my question is: How can I implement the above mentioned workaround?I have a workaround using an interval timer with try/catch, which tries to set element.currentTime to an appropriate value, and then finally calls skipTo() after exceptions stop.
What I've set up so far is the following replacement:
Code: Select all
audioplayer.load()
$(audioplayer).on('loadeddata', function() {
global tryplayinterval = setInterval(tryplay(),100)
function tryplay() {
try {
audioplayer.currentTime = 123
audioplayer.play()
clearInterval(tryplayinterval) // Success, stop interval!
} catch (e) {}
}
})