tick.js 2.59 KB
/**
 * [秒抢页面js]
 * author: 陈峰<feng.chen@yoho.cn>
 * date: 2016/09/08
 */

var $ = require('yoho-jquery'),
    tip = require('../plugin/tip'),
    seckillObj = {};

var timeObj = '';

var offsetTime = 0;

var nowTime = Date.parse(new Date()) / 1000;

var startTime = 0;

var endTime = 0;

var dateText = 0,
    newDate = 0,
    newMonth = 0,
    newDay = 0,
    newHour = 0,
    newMinus = 0;

require('../common');

seckillObj = {
    el: {
        iScroll: null,
        currentTick: null
    },

    startTick: function(elem, offsetTime) {
        var that = this,
            $el = this.el,
            hour = parseInt(offsetTime / (60 * 60), 10),
            minute = parseInt(offsetTime % (60 * 60) / 60, 10),
            second = offsetTime % 60;

        if (offsetTime) {
            $(elem).find('.tick.hour').text(hour < 0 ? '00' : (hour < 10 ? ('0' + hour) : hour));
            $(elem).find('.tick.minute').text(minute < 0 ? '00' : (minute < 10 ? ('0' + minute) : minute));
            $(elem).find('.tick.second').text(second < 0 ? '00' : (second < 10 ? ('0' + second) : second));
            if (offsetTime <= 0) { // 结束倒计时刷新状态
                window.location.reload();
            } else {
                $el.currentTick = setTimeout(function() {
                    that.startTick(elem, --offsetTime);
                }, 1000);
            }
        }

    }
};

$(
    function() {
        var ajaxUrl = '/product/detail/seckillData/' + $('.productSkn-text').val();
        $.ajax({
            type: 'GET',
            url: ajaxUrl,
            success: function(data) {

                startTime = data.startTime;

                endTime = data.endTime;

                if (startTime > nowTime) {
                    offsetTime = startTime - nowTime;
                    timeObj = $('.seckill-count-num');
                } else if (nowTime > startTime && nowTime < endTime) {
                    offsetTime = endTime - nowTime;
                    timeObj = $('.end-time');
                }
                dateText = Number(data.startTime * 1000);
                newDate = new Date(dateText);
                newMonth = newDate.getMonth() + 1;
                newDay = newDate.getDate();
                newHour = newDate.getHours();
                newMinus = newDate.getMinutes();
                $('.notStart').find('.seckill-time-c').text(newMonth + '月' + newDay + '日' + newHour + ':' + newMinus);
                seckillObj.startTick(timeObj, offsetTime);
            },
            error: function() {
                tip.show('网络异常~');
            }
        });

    }
);