Jennifer
12/22/2017 - 7:22 AM

使用promise实现倒计时

使用promise实现倒计时

const countdown = (countdown) => {
    const deferred = $.Deferred();
    const interval = setInterval(() => {
        if (countdown === 0) {
            clearInterval(interval);
            deferred.resolve();
        }
        deferred.notify(countdown--);
    }, 1000);
    return deferred.promise();
}

const $target = $('#jp-target');
countdown(60).progress((d) => {
      $target.html(`(${d}s)重发验证码`);
}).done(() => {
    $target.removeAttr('disabled');
    $target.html('发送验证码');
});