我正在建立一个点击游戏,我想要一个计时器为我自动点击。我希望计时器在每次单击按钮时都能提高速度,但是在单击所述按钮5次后阻止增加计时器的速度。
这是我到目前为止所提出的:
var total = 0;
var itemValue = 0;
var autoClick = function() {
if(itemValue <= 5) {
total = total + itemValue;
}
};
$("#button").click(function() {
itemValue++;
setInterval(autoClick, 1000);
});
有什么建议吗?
答案 0 :(得分:0)
因此,您希望每秒将total
增加x值
x取决于namy如何点击“增加速度按钮”...
我知道了吗?
为了防止同时出现间隔,您必须清除之前的内容。
var total = 0;
var itemValue = 0;
var myInterval;
var autoClick = function() {
total = total + itemValue;
console.log(total);
};
$("#button").click(function() {
if(itemValue <= 4) {
// Increase...
itemValue++;
// Prevents multiple intervals.
clearInterval(myInterval);
myInterval = setInterval(autoClick, 1000/itemValue);
console.log("Increasing by "+itemValue+" now...");
}else{
console.log("Hey! By "+itemValue+" is way enought!");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="button">Increase speed!</button>