installment.page.js 2.78 KB
var $ = require('yoho-jquery'),
    ellipsis = require('yoho-mlellipsis'),
    tip = require('../plugin/tip'),
    loading = require('../plugin/loading');

var winH = $(window).height(),
    $installmentOnly = $('.installment-only'),
    searching = false,
    page = 0;

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

ellipsis.init();

function search() {
    if (searching) {
        return;
    }
    searching = true;

    loading.showLoadingMask();

    $.ajax({
        type: 'GET',
        url: '/product/recom/maylike?gender=1,3',
        data: {
            page: page + 1
        },
        success: function(data) {

            if (data === '') {
                loading.hideLoadingMask();
            } else {
                searching = false;
                $('#goods-list').append(data);

                loading.hideLoadingMask();
                page++;

                $('.good-detail-text .name').each(function() {
                    var $this = $(this),
                        $title = $this.find('a');

                    $title[0].mlellipsis(2);
                });
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
            searching = false;
            loading.hideLoadingMask();
        }
    });
}

function scrollHandler() {
    if ($(window).scrollTop() + winH >= $(document).height() - 200) {
        search();
    }
}

// srcoll to load more
$(window).scroll(function() {
    if ($installmentOnly.length > 0) {
        window.requestAnimationFrame(scrollHandler);
    }
});


if ($installmentOnly.length > 0) {
    $('.installment-page').css({
        background: '#efefef'
    });

    search();
}

/**
 *  倒计时
 *
 * @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;
});

require.ensure([], function() { // this syntax is weird but it works
});