淘宝定时抢东西用的
必须先输入完正确密码,考虑上了网络延迟,程序提前一秒钟点击确认,可自行修改:
function checkTime() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var formattedTime = (hours < 10 ? '0' : '') + hours + ':' + (minutes < 10 ? '0' : '') + minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
console.log(formattedTime);
if (isTimeInRange(now)) {
console.log('时间到!');
var button = document.getElementById('validateButton');
if (button) {
console.log('点击一次确认按钮..');
button.click();
} console.log('没找到确认的按钮!');
}
}
function isTimeInRange(time) {
var startTime = new Date(time);
startTime.setHours(10, 59, 59, 0);
var endTime = new Date(startTime.getTime() + 2000);
return time >= startTime && time <= endTime;
}
var interval = setInterval(checkTime, 100);
搞个本地的模拟网页可测试:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Button Click</title>
<script>
function displayAlert() {
var now = new Date();
var time = now.toLocaleString();
alert("抢购成功\n" + time);
}
</script>
</head>
<body>
<input type="button" id="validateButton" class="enable" value="确定" style="margin-left: 0; font-family: 'tahoma,arial,Hiragino Sans GB,宋体'" onclick="displayAlert()">
</body>
</html>