javascript - Speedy setInterval -
i'm trying out setinterval (first time) increase score , it's working great, except reason keeps speeding up.
in sample below, every time click button, score increases more quickly. it's 50 decreasing, why?
any appreciated, thanks.
var scoretimer; var myscore = 0; var scoreincrement = 0; function increasescore() { scorecountdown(10); } //scoring function scorecountdown(scoreamt) { scoreincrement = scoreamt scoretimer = setinterval(updatescore, 50); } //updates score on set interval function updatescore() { if (scoreincrement > 0) { myscore += 1 scoretext.innerhtml = "score " + myscore; --scoreincrement } else { clearinterval(updatescore) } }
<button onclick="increasescore()">add score</button> <br> <span id="scoretext">000</span>
it's because clearinterval
wasn't called properly. fix that, change
clearinterval(updatescore)
to
clearinterval(scoretimer)
Comments
Post a Comment