Authored by 张丽霞

订单详情添加倒计时JS文件

Signed-off-by: 🍓 <lixia.zhang@yoho.cn>
@@ -8,88 +8,82 @@ var $ = require('jquery'), @@ -8,88 +8,82 @@ var $ = require('jquery'),
8 lazyLoad = require('yoho.lazyload'), 8 lazyLoad = require('yoho.lazyload'),
9 Hammer = require('yoho.hammer'), 9 Hammer = require('yoho.hammer'),
10 dialog = require('./dialog'), 10 dialog = require('./dialog'),
11 - tip = require('../plugin/tip');  
12 -  
13 -var orderId = $('#order-detail').data('id');  
14 -  
15 -var $countDownHours = $('.hours'),  
16 - $countdownContainer = $('.count-down');  
17 -var optHammer;  
18 -  
19 -downCount($countDownHours.text()); 11 + tip = require('../plugin/tip'),
  12 + orderId = $('#order-detail').data('id'),
  13 + $countDownHours = $('.hours'),
  14 + $countdownContainer = $('.count-down'),
  15 + optHammer;
20 16
21 lazyLoad({ 17 lazyLoad({
22 try_again_css: 'order-failure' 18 try_again_css: 'order-failure'
23 }); 19 });
24 20
25 -function downCount (options, callback) {  
26 - var settings = new Date(options);  
27 -  
28 - // Throw error if date is not set  
29 - if (!settings) {  
30 - $.error('Date is not defined.');  
31 - }  
32 -  
33 - // Throw error if date is set incorectly  
34 - if (!Date.parse(settings)) {  
35 - $.error('Incorrect date format, it should look like this, 12/24/2012 12:00:00.');  
36 - }  
37 -  
38 - // Save container  
39 - var container = this; 21 +function downCount(options) {
  22 + var settings = new Date(options),
  23 + targetDateFunction = function() {
  24 + var orderDate = new Date(settings),
40 25
41 - var targetDate = function () {  
42 - var order_date = new Date(settings);  
43 - //结束时间在下单时间加  
44 - var utc = order_date.getTime() + 7200000; 26 + //结束时间在下单时间加两小时
  27 + utc = orderDate.getTime() + 7200000,
  28 + newDate = new Date(utc);
45 29
46 - var new_date = new Date(utc)  
47 -  
48 - return new_date;  
49 - }; 30 + return newDate;
  31 + },
50 32
51 /** 33 /**
52 * Change client's local date to match offset timezone 34 * Change client's local date to match offset timezone
53 * @return {Object} Fixed Date object. 35 * @return {Object} Fixed Date object.
54 */ 36 */
55 - var currentDate = function () { 37 + currentDateFunction = function() {
  38 +
56 // get client's current date 39 // get client's current date
57 var date = new Date(); 40 var date = new Date();
  41 +
58 // turn date to utc 42 // turn date to utc
59 var utc = date.getTime(); 43 var utc = date.getTime();
  44 +
60 // set new Date object 45 // set new Date object
61 - // var new_date = new Date(utc + (3600000*settings.offset))  
62 return utc; 46 return utc;
63 }; 47 };
64 48
  49 + // Throw error if date is not set
  50 + if (!settings) {
  51 + $.error('Date is not defined.');
  52 + }
  53 +
  54 + // Throw error if date is set incorectly
  55 + if (!Date.parse(settings)) {
  56 + $.error('Incorrect date format, it should look like this, 12/24/2012 12:00:00.');
  57 + }
  58 +
65 /** 59 /**
66 * Main downCount function that calculates everything 60 * Main downCount function that calculates everything
67 */ 61 */
68 - function countdown () {  
69 - var target_date = targetDate(),// set target date  
70 - current_date = currentDate(); // get fixed current date  
71 -  
72 - // difference of dates  
73 - var difference = target_date - current_date; 62 + function countdown() {
  63 + var targetDate = targetDateFunction(),// set target date
  64 + currentDate = currentDateFunction(), // get fixed current date
  65 + difference = targetDate - currentDate,// difference of dates
  66 + // basic math variables
  67 + _second = 1000,
  68 + _minute = _second * 60,
  69 + _hour = _minute * 60,
  70 + _day = _hour * 24,
  71 + interval,
  72 + days,
  73 + hours,
  74 + minutes,
  75 + seconds;
74 76
75 // if difference is negative than it's pass the target date 77 // if difference is negative than it's pass the target date
76 if (difference < 0) { 78 if (difference < 0) {
  79 +
77 // stop timer 80 // stop timer
78 clearInterval(interval); 81 clearInterval(interval);
79 -  
80 - if (callback && typeof callback === 'function') callback();  
81 -  
82 return; 82 return;
83 } 83 }
84 84
85 - // basic math variables  
86 - var _second = 1000,  
87 - _minute = _second * 60,  
88 - _hour = _minute * 60,  
89 - _day = _hour * 24;  
90 -  
91 // calculate dates 85 // calculate dates
92 - var days = Math.floor(difference / _day), 86 + days = Math.floor(difference / _day),
93 hours = Math.floor((difference % _day) / _hour), 87 hours = Math.floor((difference % _day) / _hour),
94 minutes = Math.floor((difference % _hour) / _minute), 88 minutes = Math.floor((difference % _hour) / _minute),
95 seconds = Math.floor((difference % _minute) / _second); 89 seconds = Math.floor((difference % _minute) / _second);
@@ -99,13 +93,17 @@ function downCount (options, callback) { @@ -99,13 +93,17 @@ function downCount (options, callback) {
99 hours = (String(hours).length >= 2) ? hours : '0' + hours; 93 hours = (String(hours).length >= 2) ? hours : '0' + hours;
100 minutes = (String(minutes).length >= 2) ? minutes : '0' + minutes; 94 minutes = (String(minutes).length >= 2) ? minutes : '0' + minutes;
101 seconds = (String(seconds).length >= 2) ? seconds : '0' + seconds; 95 seconds = (String(seconds).length >= 2) ? seconds : '0' + seconds;
  96 +
102 // set to DOM 97 // set to DOM
103 $countdownContainer.removeClass('hide'); 98 $countdownContainer.removeClass('hide');
104 $countDownHours.text('剩余' + hours + ':' + minutes + ':' + seconds); 99 $countDownHours.text('剩余' + hours + ':' + minutes + ':' + seconds);
105 - }; 100 + }
  101 +
106 // start 102 // start
107 - var interval = setInterval(countdown, 1000);  
108 -}; 103 + setInterval(countdown, 1000);
  104 +}
  105 +
  106 +downCount($countDownHours.text());
109 107
110 //订单删除 108 //订单删除
111 optHammer = new Hammer(document.getElementsByClassName('opt')[0]); 109 optHammer = new Hammer(document.getElementsByClassName('opt')[0]);