Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Javascript function to check if time has elapsed between two dates
// lastUpdate param should be a date object or a number representing milliseconds since 1970
// interval param should be seconds
// Interval is the elapsed time you are checking
// Returns true if the interval time has elapsed
// Returns false if the interval has not elapsed
function update(lastUpdate, interval) {
var curTime = new Date().getTime();
lastUpdate = (typeof lastUpdate.getTime == "function")? lastUpdate.getTime(): lastUpdate;
interval *= 1000;
curTime -= interval;
return (lastUpdate >= curTime) ? false: true;
// You could do more typechecking on the lastUpdate param if you are unsure of the data you are passing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment