订单详情添加倒计时JS文件
Signed-off-by: 🍓 <lixia.zhang@yoho.cn>
Showing
1 changed file
with
71 additions
and
73 deletions
@@ -8,104 +8,102 @@ var $ = require('jquery'), | @@ -8,104 +8,102 @@ 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); | 21 | +function downCount(options) { |
22 | + var settings = new Date(options), | ||
23 | + targetDateFunction = function() { | ||
24 | + var orderDate = new Date(settings), | ||
27 | 25 | ||
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 | - } | 26 | + //结束时间在下单时间加两小时 |
27 | + utc = orderDate.getTime() + 7200000, | ||
28 | + newDate = new Date(utc); | ||
37 | 29 | ||
38 | - // Save container | ||
39 | - var container = this; | ||
40 | - | ||
41 | - var targetDate = function () { | ||
42 | - var order_date = new Date(settings); | ||
43 | - //结束时间在下单时间加 | ||
44 | - var utc = order_date.getTime() + 7200000; | ||
45 | - | ||
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 | ||
65 | - /** | ||
66 | - * Main downCount function that calculates everything | ||
67 | - */ | ||
68 | - function countdown () { | ||
69 | - var target_date = targetDate(),// set target date | ||
70 | - current_date = currentDate(); // get fixed current date | 49 | + // Throw error if date is not set |
50 | + if (!settings) { | ||
51 | + $.error('Date is not defined.'); | ||
52 | + } | ||
71 | 53 | ||
72 | - // difference of dates | ||
73 | - var difference = target_date - current_date; | 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 | + } | ||
74 | 58 | ||
75 | - // if difference is negative than it's pass the target date | ||
76 | - if (difference < 0) { | ||
77 | - // stop timer | ||
78 | - clearInterval(interval); | 59 | + /** |
60 | + * Main downCount function that calculates everything | ||
61 | + */ | ||
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; | ||
76 | + | ||
77 | + // if difference is negative than it's pass the target date | ||
78 | + if (difference < 0) { | ||
79 | + | ||
80 | + // stop timer | ||
81 | + clearInterval(interval); | ||
82 | + return; | ||
83 | + } | ||
79 | 84 | ||
80 | - if (callback && typeof callback === 'function') callback(); | 85 | + // calculate dates |
86 | + days = Math.floor(difference / _day), | ||
87 | + hours = Math.floor((difference % _day) / _hour), | ||
88 | + minutes = Math.floor((difference % _hour) / _minute), | ||
89 | + seconds = Math.floor((difference % _minute) / _second); | ||
90 | + | ||
91 | + // fix dates so that it will show two digets | ||
92 | + days = (String(days).length >= 2) ? days : '0' + days; | ||
93 | + hours = (String(hours).length >= 2) ? hours : '0' + hours; | ||
94 | + minutes = (String(minutes).length >= 2) ? minutes : '0' + minutes; | ||
95 | + seconds = (String(seconds).length >= 2) ? seconds : '0' + seconds; | ||
96 | + | ||
97 | + // set to DOM | ||
98 | + $countdownContainer.removeClass('hide'); | ||
99 | + $countDownHours.text('剩余' + hours + ':' + minutes + ':' + seconds); | ||
100 | + } | ||
81 | 101 | ||
82 | - return; | ||
83 | - } | 102 | + // start |
103 | + setInterval(countdown, 1000); | ||
104 | +} | ||
84 | 105 | ||
85 | - // basic math variables | ||
86 | - var _second = 1000, | ||
87 | - _minute = _second * 60, | ||
88 | - _hour = _minute * 60, | ||
89 | - _day = _hour * 24; | ||
90 | - | ||
91 | - // calculate dates | ||
92 | - var days = Math.floor(difference / _day), | ||
93 | - hours = Math.floor((difference % _day) / _hour), | ||
94 | - minutes = Math.floor((difference % _hour) / _minute), | ||
95 | - seconds = Math.floor((difference % _minute) / _second); | ||
96 | - | ||
97 | - // fix dates so that it will show two digets | ||
98 | - days = (String(days).length >= 2) ? days : '0' + days; | ||
99 | - hours = (String(hours).length >= 2) ? hours : '0' + hours; | ||
100 | - minutes = (String(minutes).length >= 2) ? minutes : '0' + minutes; | ||
101 | - seconds = (String(seconds).length >= 2) ? seconds : '0' + seconds; | ||
102 | - // set to DOM | ||
103 | - $countdownContainer.removeClass('hide'); | ||
104 | - $countDownHours.text('剩余' + hours + ':' + minutes + ':' + seconds); | ||
105 | - }; | ||
106 | - // start | ||
107 | - var interval = setInterval(countdown, 1000); | ||
108 | -}; | 106 | +downCount($countDownHours.text()); |
109 | 107 | ||
110 | //订单删除 | 108 | //订单删除 |
111 | optHammer = new Hammer(document.getElementsByClassName('opt')[0]); | 109 | optHammer = new Hammer(document.getElementsByClassName('opt')[0]); |
-
Please register or login to post a comment