installment.starting-service.page.js 1.29 KB
const $ = require('yoho-jquery');

const Timer = function() {
    this.counter = 0;
    this.countdownTimer = null;
};


/**
 *  倒计时
 *
 * @param progress 进度回调
 * @param complete 完成回调
 */
Timer.prototype.startCountdown = function(progress, complete) {
    if (this.counter > 0 || this.countdownTimer) {
        return;
    } else {
        this.counter = 59;
    }

    if (progress) {
        progress.call(this, this.counter);
    }

    this.countdownTimer = setInterval(()=> {
        this.counter--;

        if (this.counter <= 0) {
            if (complete) {
                clearInterval(this.countdownTimer);

                this.counter = 0;
                this.countdownTimer = null;
                complete.call(this);
            }
        }

        if (progress && this.counter > 0) {
            progress.call(this, this.counter);
        }
    }, 1000);
};


/**
 * 点击发送短信事件
 */
$('#send-sms').click(function() {
    new Timer().startCountdown(function(counter) {
        $('#send-sms').text(counter + 's');
    }, function() {
        $('#send-sms').text('获取验证码');
    });

    return false;
});

$('#card-no').keydown(function() {
    const value = $(this).val();

    $(this).val(value.replace(/\s[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 '));
});