Skip to content

Instantly share code, notes, and snippets.

@nuxodin
Created December 13, 2012 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuxodin/4277472 to your computer and use it in GitHub Desktop.
Save nuxodin/4277472 to your computer and use it in GitHub Desktop.
Vibration Polyfill using sound (prototype)
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