Created
December 13, 2012 16:07
-
-
Save nuxodin/4277472 to your computer and use it in GitHub Desktop.
Vibration Polyfill using sound (prototype)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
navigator.q1Vibrate = function(){ | |
var native = navigator.vibrate || navigator.mozVibrate || navigator.wekbitVibrate; | |
if(native){ | |
return native; | |
} | |
var scripts = document.getElementsByTagName('script'); | |
var script = scripts[scripts.length-1]; | |
var path = script.src.replace(/vibrate.js/,''); | |
var mp3File = path+'vibrate.mp3'; | |
var audioEl = new Audio(mp3File); | |
audioEl.setAttribute('loop','loop'); | |
return function(arg){ | |
var durations = arg.length ? arg : [arg]; | |
var counter = 0; | |
function next(){ | |
stop(); | |
if(!durations.length){return;} | |
++counter%2===1 && play(); | |
setTimeout( next, durations.shift() ); | |
} | |
next(); | |
function play(){ | |
audioEl.muted = true; | |
audioEl.play(); | |
audioEl.muted = false; | |
} | |
function stop(){ | |
audioEl.muted = true; | |
audioEl.currentTime = 0; | |
audioEl.pause(); | |
} | |
}; | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment