Created
December 9, 2013 14:05
-
-
Save beshur/7872704 to your computer and use it in GitHub Desktop.
jQuery Simple minutes countdown timer
This file contains hidden or 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
// jQuery Simple minutes countdown timer | |
// https://gist.github.com/beshur/7872704 | |
// @beshur | |
$.fn.countdownize = function() { | |
if (typeof window.page_var == "undefined") window.page_var = {}; | |
this.each(function(i) { | |
var obj = $(this); | |
// assumes integer seconds are here | |
var delta = obj.data("time"); | |
var minutes = Math.floor(delta / 60) % 60; | |
var seconds = delta - minutes*60; | |
obj.tpl = '<span class="e_digit">{ins}</span>' | |
obj.data("curtime", delta); | |
page_var.timer = {}; | |
page_var.timer[i] = window.setInterval(function() { | |
var delta = obj.data("curtime") - 1 || 0; | |
obj.data("curtime", delta); | |
var minutes = Math.floor(delta / 60) % 60; | |
var seconds = delta - minutes*60; | |
minutes = minutes.toString(); | |
seconds = seconds.toString(); | |
var html = ''; | |
if (minutes.length > 1) { | |
html += obj.tpl.replace("{ins}", minutes[0]); | |
html += obj.tpl.replace("{ins}", minutes[1]); | |
} else { | |
html = obj.tpl.replace("{ins}", minutes[0]); | |
} | |
html += obj.tpl.replace("{ins}", ":"); | |
if (seconds.length < 2) { | |
html += obj.tpl.replace("{ins}", "0"); | |
html += obj.tpl.replace("{ins}", seconds[0]); | |
} else { | |
html += obj.tpl.replace("{ins}", seconds[0]); | |
html += obj.tpl.replace("{ins}", seconds[1]); | |
} | |
obj.html(html).removeClass("lock"); | |
if (delta == 0) { | |
window.clearInterval(page_var.timer[i]); | |
return; | |
} | |
}, 1000); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment