0

I am trying to create a custom confirm box to match look & feel of my website here is my code...

customconfirm.js

function CustomConfirm(dialog){
console.log("in CustomConfirm")
var result;
console.log("in render")
var winW = window.outerWidth;
var winH = window.outerHeight;
var dialogoverlay = document.getElementById('dialogoverlay');    
var dialogbox = document.getElementById('dialogbox');
dialogoverlay.style.display = "block";
dialogoverlay.style.height = winH+"px";
dialogbox.style.left = (winW/2) - (550 * 0.5)+"px";
dialogbox.style.top = "250px";
dialogbox.style.display = "block";
document.getElementById('dialogboxhead').innerHTML = '<img src="" alt="logo" height="20" width="20"> Heading';
document.getElementById('dialogboxbody').innerHTML = dialog;
document.getElementById('dialogboxfoot').innerHTML = '<button onclick="Confirmyes()">Yes</button><button onclick="Confirmno()">No</button>';
console.log("render end")}

function Confirmno(){
console.log("in no")    
document.getElementById('dialogbox').style.display = "none"; 
document.getElementById('dialogoverlay').style.display = "none";
processResult(false);
console.log("no ended");}

function Confirmyes(){
console.log("in yesss")
document.getElementById('dialogbox').style.display = "none"; 
document.getElementById('dialogoverlay').style.display = "none";
processResult(true);
console.log("yes ended");}

function processResult(r) {
result = r;
alert(result);
return result;}

I am calling my function as below:

var r = CustomConfirm("Do you really want to delete Player\n'" + player_name + "' ?");
if (r == true){
    console.log(r);
}
elseif (r == false){
    console.log(r);
}
console.log("Result = " + r);

whenever I am calling CustomConfirm, it is always showing 'Result = Undefined' in console.(rather than true or false), means it is not waiting for user selection of yes or no.

How can I pause the CustomConfirm function so that i can store the value in r & do next thing with that value?

devlin31
  • 81
  • 10
  • `confirm` is synchronous. It blocks the flow of execution until the user has clicked a button on the dialog. You cannot duplicate this behavior. Your dialog is always going to work asynchronously. There are a *multitude* of questions on this exact problem. Your dialog needs to either accept success/failure callbacks, or return a promise, or any of another host of things asynchronous code does. – user229044 Oct 07 '14 at 07:24
  • Just looking at it, but it seems to me that the `result` is not outside of these functions and maybe that is the problem, it is not the same variable like you expect it to be. – Rudy Oct 07 '14 at 07:27
  • Duplicate: http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – user229044 Oct 07 '14 at 17:36

0 Answers0