Question

js to create confirm messages. I building a web app where the user can change 2 boundaries of a graph(using update button). When The user click the update button, a confirm message is appeared and asked from the user to confirm this change.

The problem is that when I click 'ok' or 'cancel' it doesn't matter and doesn't change the flag and the change of boundaries take place anyway(because the init value of the flag is true).From messages of console.log, I have learn that the app doesn't wait for user respond and continue to perform the other lines of code... I have tried to put all my code in the alertify function(e) which I sent and still it doesn't work for me. This my code:

Alarm.prototype.updateAlarm=function(_limits){
var n=null;
var str="";
var changed_flag=false;
//create confirm dialog:
var engLimitMsg_1=" Upper Limit:";
var engLimitMsg_2=" Lower Limit:";
var hebLimitMsg_1=" גבול עליון:";
var hebLimitMsg_2=" גבול תחתון:";
var user_confirm=true;
var confirmMSG="";
if(_limits.length>=2){
    if(this.language_mode==HEB_MODE){
        confirmMSG+=ENG_CONFIRM_MSG;
        if(IsNumeric(parseInt(_limits[0]))){
            confirmMSG+=engLimitMsg_1+_limits[0];
        }
        if(IsNumeric(parseInt(_limits[1]))){
            confirmMSG+=engLimitMsg_2+_limits[1];

        }
    }
    else{
        confirmMSG+=ENG_CONFIRM_MSG;
        if(IsNumeric(parseInt(_limits[0]))){
            confirmMSG+=engLimitMsg_1+_limits[0];
        }
        if(IsNumeric(parseInt(_limits[1]))){
            confirmMSG+=engLimitMsg_2+_limits[1];

        }

    }

    alertify.confirm(confirmMSG, function (e) {
        if (e) {
            // user clicked "ok"
            user_confirm=true;
            console.log("user clicked ok");

        } else {
            // user clicked "cancel"
            user_confirm=false;
            console.log("user clicked cancel");

        }
    });
     //user has confirm the change!
    if(user_confirm){
        for (var i=0;i<_limits.length;i++){
            if(IsNumeric(parseInt(_limits[i]))){
                n=this.msgs[i].indexOf(this.limits[i].toString());
                this.limits[i]=_limits[i];
                this.msgs[i]=this.msgs[i].slice(0,n)+this.limits[i];
                changed_flag=true;
            }
        }
    }
    // clean past alerts in case of change limits.
    if(changed_flag)
        this.cleanPastAlerts();
    if(DEBUG_MODE)
        console.log("alarms update: upperLimit:"+this.limits[0]+" lowerLimit:"+this.limits[1]);

}
else{
    console.log("Error occurred!, array length is less than 2");
}

};

Note: This the Error which I get sometimes:

Uncaught TypeError: Cannot read property 'removeEventListener' of undefined 
Was it helpful?

Solution

The js confirm from js in the browser stops the js execution. When you're using a "custom confirm" (a js library confirm) the code below the "custom confirm" keeps executing and that's the reason why you obtain the message there, as it keeps executing and you have variables defined inside the function, when js tries to use them, it shows the "undefinned variable".

You have to insert all the code in the alertfy to make sure it works correctly (I haven't checked code below, so, just take it carefully):

alertify.confirm(confirmMSG, function (e) {
    if (e) {
        // user clicked "ok"
        user_confirm=true;
        console.log("user clicked ok");

        for (var i=0;i<_limits.length;i++){
          if(IsNumeric(parseInt(_limits[i]))){
            n=this.msgs[i].indexOf(this.limits[i].toString());
            this.limits[i]=_limits[i];
            this.msgs[i]=this.msgs[i].slice(0,n)+this.limits[i];
            changed_flag=true;
        }
        // clean past alerts in case of change limits.
        if(changed_flag) {
          this.cleanPastAlerts();
        }
      }
    } else {
        // user clicked "cancel"
        user_confirm=false;
        console.log("user clicked cancel");

    }
});
if(DEBUG_MODE)
    console.log("alarms update: upperLimit:"+this.limits[0]+" lowerLimit:"+this.limits[1]);

OTHER TIPS

Could you use JavaScript's built in confirm message box? If so it's quite simple.

if (confirm("Press OK to accept"))
    alert("You accepted!");
else
    alert("You declined!");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top