JavaScript - Try/Catch within setInterval()

For everything that's not in any way related to PureBasic. General chat etc...
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

JavaScript - Try/Catch within setInterval()

Post by c4s »

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:

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()
})
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:
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.
So my question is: How can I implement the above mentioned workaround?

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) {}
	}
})
What do you think? Will this work correctly or do you spot any errors I overlooked?
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: JavaScript - Try/Catch within setInterval()

Post by DarkDragon »

You need more semicolons. Beside this, it should work, although its not a very clean way to do it (normally you would check the exception and only eat a special exception type).
bye,
Daniel
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: JavaScript - Try/Catch within setInterval()

Post by c4s »

I was astonished myself of the lack of semicolos. Maybe it's allowed if it's a javascript-only file?!

Anyway, I tested this code part:

Code: Select all

audioplayer.load()
$(audioplayer).on('loadeddata', function() {
	tryplayinterval = setInterval(function(){tryplay()}, 100)
	function tryplay() {
		try {
			audioplayer.currentTime = 123
			audioplayer.play()
			clearInterval(tryplayinterval)  // Success, stop interval!
		} catch (e) {}
	}
})
Unfortunately it didn't fix the issue at all, although in my understanding trying multiple times should do the trick. It seems that Internet Explorer simply doesn't want to set the currentTime...
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Post Reply