所以我正在为这个正在开展工作的网页开发这个商店系统
但是我把它添加到js
之后function s1u1f() {
if (S1U1 === false && script1B === true && Points >= 100) {
Points = Points - 100;
clearInterval(script1);
s1ut.innerHTML = "UPGRADE2";
ids1.innerHTML = "this will speed <br /> up script1 <br /> to 0,500s";
s1p.innerHTML = "Price: 125P";
script1 = setInterval(function() {click(1);}, 750);
S1U1B.removeEventListener("click", s1u1f());
}else if (100 > Points) {
alert("you dont have enough points to buy this item");
}else if (script1B === false) {
alert("You need to buy script1 first");
}
}
它会自动激活else if(100 > Points)
如果你想看看我的意思,请转到此链接 http://clickthebutton.esy.es/clickthebutton%20layout%20demo/cssTest.html
答案 0 :(得分:0)
问题在于您创建点击处理程序的方式:
S1U1B.addEventListener("click", s1u1f());
您需要传入函数引用,而不是立即调用该函数。删除功能上的parens:
S1U1B.addEventListener("click", s1u1f);
答案 1 :(得分:0)
此行(上面未显示)是问题所在:
S1U1B.addEventListener("click", s1u1f());
立即调用s1u1f()
函数。启动时有0
个点,因此100 > Points
,您会看到alert()
。
你想传递这个功能,而不是叫它:
S1U1B.addEventListener("click", s1u1f);
您还想要改变:
S1U1B.removeEventListener("click", s1u1f());
到
S1U1B.removeEventListener("click", s1u1f);
出于同样的原因。