Skip to content

Instantly share code, notes, and snippets.

@videlais
Created December 26, 2013 07:16
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 videlais/8130778 to your computer and use it in GitHub Desktop.
Save videlais/8130778 to your computer and use it in GitHub Desktop.
Vibrate.js
/**
* A Vibrate object for detecting and using the window.navigator.vibrate
* function.
*
* Code borrows from David Walsh's work
* http://davidwalsh.name/vibration-api
*
* @property {boolean} supported If vibrate is supported in the current context
*/
var Vibrate = (function(self) {
self.supported = ('vibrate' in window.navigator);
var interval = null;
/*
* Starts vibration for given duration
* @param duration Amount of time in ms to vibrate
*/
self.startVibrate = function(duration) {
if (self.supported) {
window.navigator.vibrate(duration);
}
};
/*
* Clears the interval, if set, and stops any current vibration
*/
self.stopVibrate = function() {
if (self.supported) {
if (interval !== null) {
clearInterval(interval);
}
window.navigator.vibrate(0);
}
};
/*
* Starts a peristent vibration for the given duration and interval
* @param duration The amount of time to vibrate in ms
* @param interval The amount of timne to pause between vibrations in ms
*/
self.startPeristentVibrate = function(duration, interval) {
interval = window.setInterval(function() {
startVibrate(duration);
}, interval);
};
return self;
})(Vibrate || {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment