1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>倒计时抢购</title> </head> <body> <div id="box"></div> <script> ~function(){ let box = document.getElementById('box'), serverTime = null, autoTimer = null; let formatTime = (value) => { value < 10 ? value = `0${value}` : null; return value }; let fn = () => { serverTime += 1000; let startTime = new Date('2019/6/4 15:31:08').getTime(), remainTime = startTime - serverTime; if (remainTime < 0) { box.innerHTML = '开抢啦!'; clearInterval(autoTimer); } else { let hours = Math.floor(remainTime / (1000 * 60 * 60)); remainTime -= hours * 1000 * 60 * 60; let minute = Math.floor(remainTime / (1000 * 60)); remainTime -= minute * 1000 * 60; let second = Math.floor(remainTime / 1000); hours = formatTime(hours); minute = formatTime(minute); second = formatTime(second); box.innerHTML = `距离抢购还有:${hours}:${minute}:${second}`; } };
let getServeTime = () => { let xhr = new XMLHttpRequest(); xhr.onreadystatechange = () => { if (!/^(2|3)\d{2}$/.test(xhr.status)) return; if (xhr.readyState === 2) { serverTime = new Date(xhr.getResponseHeader('date')).getTime(); fn(); autoTimer = setInterval(fn, 1000); } }; xhr.open('head', 'data.json', true); xhr.send(); }; getServeTime(); }();
</script> </body> </html>
|