Skip to content

Instantly share code, notes, and snippets.

@GZShi
Created May 10, 2016 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GZShi/11a3966f5708079ac4d693f2455c747a to your computer and use it in GitHub Desktop.
Save GZShi/11a3966f5708079ac4d693f2455c747a to your computer and use it in GitHub Desktop.
(function () {
if (location.host !== 'shop.48.cn') {
console.error('kidding me?');
return ;
}
if (typeof jQuery !== 'function') {
console.error('缺少基础库');
return ;
}
if (typeof Promise !== 'function') {
console.error('请升级浏览器');
return ;
}
var seat = {
'vip坐票': '2',
'普通坐票': '3',
'普通站票': '4'
};
function buy(ticketId, ticketsCount, seatType) {
ticketId = ticketId || /\/(\d+)$/.exec(location.href)[1];
ticketsCount = ticketsCount || 1;
seatType = seatType || '普通坐票';
return new Promise(function (resolve, reject) {
$.ajax({
url: '/TOrder/add',
type: 'post',
dataType: 'json',
data: {
id: ticketId,
num: ticketsCount,
seattype: seat[seatType] || '3',
brand_id: '1',
r: Math.random()
},
error: reject,
success: function (result) {
if (result.HasError) {
reject(result.Message);
}
else {
resolve(result.ReturnObject);
}
}
});
});
}
function delay(tick) {
return new Promise(function (resolve, reject) {
setTimeout(resolve, tick);
});
}
/////////// 延迟计算
function getServerTime() {
return new Promise(function (resolve, reject) {
$.ajax({
type: 'GET',
url: '/TOrder/gettimestamp',
dataType: 'json',
data: {r: Math.random()},
success: function (timestamp) {
// "/Date(1462878288234)/"
var date = /\/Date\((\d+)\)/.exec(timestamp)[1];
date = +date;
resolve(date);
}
})
});
}
function calcNetDelay() {
var tStart = Date.now();
return getServerTime().then(function (servTime) {
var tEnd = Date.now();
var midPoint = (tStart + tEnd) >> 1;
var delay = servTime - midPoint;
return delay;
});
}
var tasks = (new Array(11)).join(',').split(',').map(function () {
return calcNetDelay();
});
Promise.all(tasks).then(function (delays) {
// 对延迟结果进行排序
delays.sort(function (a, b) {
return a - b;
});
// 分别去掉2个最高和最低延迟
delays.shift();
delays.shift();
delays.pop();
delays.pop();
// 取出其中最大的值作为延迟
var maxDelay = Math.max.apply(Math, delays);
console.info('计算出本地与服务器之间的延迟是', maxDelay);
// 返回取服务器时间的函数
return function () {
var now = Date.getTime();
return now + maxDelay;
};
}).then(function (getTime) {
function check(targetTime) {
function taskFn() {
return buy('106', 1, '普通坐票')
.then(function (orderId) {
if (orderId) window.location.href = orderId; // 跳到下单页面
}, function (failedReason) {
console.info(failedReason);
});
}
var now = getTime();
var delay;
if (targetTime - now > 15 * 1000) {
// 如果距离目标时间大于15s,则15s后刷一次票
delay = 15 * 1000;
} else {
// 否则,准点刷新
delay = targetTime - now;
}
console.log(delay, '秒钟后尝试一次!');
setTimeout(function () {
taskFn.then(check.bind(null, targetTime));
}, delay);
}
var checkTime = new Date('2016-05-10 20:00:00').getTime();
check(checkTime);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment