dimanche 26 février 2017

Javascript function if statement not excuted right

I have this little piece of code that is supposed to do something if some "if" conditions are met. It does not work as it should and I could not figure out why.The code is a bit lengthy please bear with me.Any kind of help is really appreciated!

First I have a button in my html,when it is click it will trigger function

function coverCard() {
if (2>1) {
GodAn();
}
else  {
if (bbbbb===0) {   do something
}
else
{    do sth 
}
}
}

This function will lead to GodAn function shown as follow

function GodAn() {
var a = 1
 if (a<2) {
 document.getElementById("coverCard").onclick =Alert.render("do option 1  please")
 bbbbb=0;
 }
 else
 {  
 document.getElementById("coverCard").onclick =Alert.render("do option 2  please")
 bbbbb=2;
 }
 }

Finally following is the function defining what is shown in the dialog box and what will happen when its"ok"button is clicked

 function CustomAlert(){
 this.render = function(dialog){
 document.getElementById('dialogboxfoot').innerHTML = '<button onclick="Alert.ok()">ok</button>';
}


     if  (bbbbb===0) {
this.ok = function() {
    alert("do option1")
    console.log(bbbbb)
     }
    }
    else this.ok = function() {

    alert("do option 2")
    console.log(bbbbb)
    }
    }   
    var Alert = new CustomAlert();

**what I expect to happen is when the html button is clicked, the dialog box will show "do option 1 please",(which it always does) and then alert "do option1". However sometimes in the CustomAlert function the "do option 2" alert will be wrongly triggered,even when the global var bbbbb is reset to 0.(console.log also confirms bbbbb is 0). I have uploaded the original html file and the link is here:*

http://ift.tt/2myFbvb This really drives me crazy so somebody please shed some light here please?

Aucun commentaire:

Enregistrer un commentaire