This code snippet is now available with a GitHub repository: href="https://github.com/guille/ms.js" rel="nofollow">https://github.com/guille/ms.js
I wanted to pass along a simple but useful module that was posted as a Gist to GitHub by Guillermo Rauch. Â This simple module, available via NPM as ms, provides a simple function for turning a human-readable string into milliseconds.
// Created by milliseconds
/**
# ms.js
No more painful `setTimeout(fn, 60 * 4 * 3 * 2 * 1 * Infinity * NaN * '☃')`.
ms('2d') // 172800000
ms('1.5h') // 5400000
ms('1h') // 3600000
ms('1m') // 60000
ms('5s') // 5000
ms('500ms') // 500
ms('100') // 100
ms(100) // 100
**/
(function (g) {
var r = /(\d*.?\d+)([mshd]+)/
, _ = {}
_.ms = 1;
_.s = 1000;
_.m = _.s * 60;
_.h = _.m * 60;
_.d = _.h * 24;
function ms (s) {
if (s == Number(s)) return Number(s);
r.exec(s.toLowerCase());
return RegExp.$1 * _[RegExp.$2];
}
g.top ? g.ms = ms : module.exports = ms;
})(this);
A very nice little gem by Guiller. Â Keep this function handy to save yourself from math expressions for time calculations!
href="http://davidwalsh.name/ms">ms: Tiny Millisecond Module by Guillermo Rauch is a post from: href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.
|
BorderMaker demo usage
(28/07/2011)
|