jquery - Change countdown timer text once value is negative -
i have countdown timer counts down days. works fine want such that, values reach negative, text changes "days left" "days since" , removes negative sign.
how can achieve this?
jquery:
// set date we're counting down var target_date = new date("aug 15, 2013").gettime(); // variables time units var days, hours, minutes, seconds; // tag element var countdown = document.getelementbyid("ribboncountdown"); var days_span = document.createelement("span"); days_span.classname = 'days'; countdown.appendchild(days_span); // update tag id "countdown" every 1 second setinterval(function () { // find amount of "seconds" between , target var current_date = new date().gettime(); var seconds_left = (target_date - current_date) / 1000; // time calculations days = parseint(seconds_left / 86400); seconds_left = seconds_left % 86400; // format countdown string + set tag value. days_span.innerhtml = '<span>' + days + '</span>' + 'days go'; //ribboncountdown.innerhtml = days + "d,; }, 1000);
update text based on whether days negative:
if (days < 0) { days_span.innerhtml = '<span>' + days * -1 + '</span>' + ' days since'; } else { days_span.innerhtml = '<span>' + days + '</span>' + ' days go'; }
fiddle: http://jsfiddle.net/z8zwc/3/
or can simplify this:
var suffix; if (days < 0) { days = days * -1; suffix = ' days since'; } else { suffix = ' days go'; } days_span.innerhtml = '<span>' + days + '</span>' + suffix;
Comments
Post a Comment