Created
December 26, 2013 07:16
-
-
Save videlais/8130778 to your computer and use it in GitHub Desktop.
Vibrate.js
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
/** | |
* 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