This is kind of fragile...  I suggest to use the "ended" event instead (See https://bugzilla.mozilla.org/show_bug.cgi?id=865233).  This event is dispatched to the AudioBufferSourceNode when playback of its buffer is finished.

Unfortunately this event is not yet supported in WebKit/Blink, so for now you might want to use it only if typeof(AudioContext)=="function" or something, and keep this code as the fallback for WebKit/Blink.