jquery - Javascript repeating when doing simple addition -


had trouble this, of things not adding correctly, in previous question, javascript variables not adding, concatenating strings, straight javascript worked fine. have built code in jsfiddle : http://jsfiddle.net/trout0525/l2hhof79/7/

finally calcscores routine repeats, version have on webpage i'm working on. here javascript think culprit, binds i'm using, i'm not sure why:

javascript:

    var selectionname= [         "100pointscol1","200pointscol1","300pointscol1","400pointscol1","500pointscol1",         "200pointscol1","200pointscol2","300pointscol2","400pointscol2","500pointscol2"     ];  var teamselection = {}; teamselection['1'] = {}; teamselection['2'] = {}; teamselection['3'] = {}; teamselection['4'] = {}; teamselection['5'] = {};  teamselection['1']['selected'] = false; teamselection['2']['selected'] = true; teamselection['3']['selected'] = false; teamselection['4']['selected'] = false; teamselection['5']['selected'] = false;  teamselection['1']['score'] = 0; teamselection['2']['score'] = 0; teamselection['3']['score'] = 0; teamselection['4']['score'] = 0; teamselection['5']['score'] = 0;    window.onload = function() {     $(function(){         (var i=0; < selectionname.length; i++) {             selection = selectionname[i];             $("#" + selection).bind("click", {sname: selection}, makeselection);         }     }); }  function makeselection(event) {     selection = event.data.sname;      $("#" + selection).unbind("click");     openquestion(selection); }    function startgame() {     showgameboard(); }  function showgameboard() {     $("#scoreboard").css("z-index", 100);     $("#gameboard").css("z-index", 100);     $("#startboard").css("z-index", 1); }  function openquestion(selection) {     $("#scoreboard").css("z-index", 1);     $("#gameboard").css("z-index",1);     $("#questionwindow").css("z-index",100);      amount = selection.substr(0, selection.length-10);      $("#questionwindow").bind("click", function(){         openanswer(amount);     }); }  function openanswer(amount){      $("#questionwindow").unbind("click");      $("#questionwindow").css("z-index",1);     $("#answerwindow").css("z-index",100);      $("#answerwindow").bind("click", function(){         calcscores(amount)         showgameboard();     }); }  function calcscores(amount) {     $("#answerwindow").css("z-index",1);      amount = parseint(amount,10);      for(var key in teamselection) {          var total = 0;          if (teamselection[key]['selected']) {             score = parseint(teamselection[key]['score'],10);             alert("score: " + score + "\n" +                   "amount: " + amount + "\n" +                   "total: " + total);             var total = +score + +amount;             alert("score: " + score + "\n" +                   "amount: " + amount + "\n" +                   "total: " + total);             teamselection[key]['score'] = total;         }     }     return; } 

so, why repeating calculation?

see updated jsfiddle: http://jsfiddle.net/l2hhof79/22/

the problem

this occurring because binding answer window's click event handler multiple times. event handler calls calcscores().

see below:

function openanswer(amount){     $("#questionwindow").unbind("click");      $("#questionwindow").css("z-index",1);     $("#answerwindow").css("z-index",100);      $("#answerwindow").bind("click", function(){         calcscores(amount)         showgameboard();     }); } 

so, every time openanswer() function runs, another event handler bound answerwindow's click event. in other words, every time make selection, calcscores() called additional time.

potential fix

the quickest fix change bind() function calls one() function calls:

$("#questionwindow").one("click", function() { //... 

and

$("#answerwindow").one("click", function() { //... 

this allow event handler bound 1 call.

another bug

there's typo in declaration selectionname variable. typo results in binding 2 event handlers 200pointscol1 selection. should see why. fixed in jsfiddle.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -