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) 

view online - https://plnkr.co/edit/0xmfc3rn5jciroypjhtq?p=preview


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -