我真的无法找出我做错了什么!
setInterval(function() {
var time = document.getElementById("timer").innerHTML;
var x = time * 1000 - 400;
setTimeout(function() {
var x2 = document.getElementById("2x-total").innerHTML;
var x3 = document.getElementById("3x-total").innerHTML;
if (x2 < 2 * x3) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
var run = document.getElementById("btn2x");
run.dispatchEvent(evt);
}
}, x);
}, 7000);
function x2a() {
document.getElementById("x2p").innerHTML = x2;
}
function x3a() {
document.getElementById("x3p").innerHTML = x3;
}
<div id="timer">01.14</div>
<button id="btn2x" onclick="x2a()" style="display: block;">2x</button>
<button id="btn3x" onclick="x3a()" style="display: block;">3x</button>
<span id="2x-total">12000</span>
<span id="3x-total">12000</span>
<p id="x2p"></p>
<p id="x3p"></p>
代码应该在循环中运行,首先等待7秒,然后运行脚本,脚本应该搜索计时器值并等到只有400毫秒并按下正确的按钮,但它崩溃了,我能做什么?办?
答案 0 :(得分:1)
问题是您在x2
匿名方法中声明了x3
和setInterval
个变量。试试这个:
var x2;
var x3;
setInterval(function() {
var time = document.getElementById("timer").innerHTML;
var x = time * 1000 - 400;
setTimeout(function() {
x2 = document.getElementById("2x-total").innerHTML;
x3 = document.getElementById("3x-total").innerHTML;
if (x2 < 2 * x3) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
var run = document.getElementById("btn2x");
run.dispatchEvent(evt);
}
}, x);
}, 7000);
function x2a() {
document.getElementById("x2p").innerHTML = x2;
}
function x3a() {
document.getElementById("x3p").innerHTML = x3;
}
答案 1 :(得分:0)
首先删除while(1){}循环。