countdown.js 745 Bytes
/**
 * 倒计时 mm:ss
 *
 * @author: liqi <qi.li@yoho.cn>
 * @date: 2016/11/11
 */

/**
 * 格式化
 * @param total 总的分钟数
 * @param el 倒计时显示元素
 * @returns {string}
 */
function _secsFormat(total, el) {
    var mins = Math.floor(total / 60);
    var secs = total - mins * 60;

    mins < 10 && (mins = '0' + mins);
    secs < 10 && (secs = '0' + secs);

    el && el.html(mins + ':' + secs);

    if (!el) {
        return mins + ':' + secs;
    }
}

function countdown(mins, el) {
    var interval,
        secs = mins * 60;

    interval = setInterval(function() {
        _secsFormat(--secs, el);
        secs === 0 && clearInterval(interval);
    }, 1000);
    _secsFormat(secs, el);
}

module.exports = countdown;