mercredi 24 février 2016

javascript - if statement excecutes both if and else blocks

This is my code:

var counter = 0;

function showAddButton() {
  var cnt = document.getElementsByClassName('img-cntnr').length;
  counter++;  // for detecting double excecution in debugger

  if (cnt < 4) {
    $('#div-add').show();
  } else {
    $('#div-add').hide();
  }
}

//someweher in my code
showAddButton();

when I call function in my code, JavaScript executes both $('#div-add').show(); and $('#div-add').hide(); lines.

I've defined a variable (counter) and watched that in debugger for detecting parallel twice call. but there is not any parallel execution and in first call if-statement executes both blocks!

how can I fix that? Thank You.

Aucun commentaire:

Enregistrer un commentaire