Skip to content Skip to sidebar Skip to footer

Javascript Confirm Function Ok And Cancel Returns Same Result

Here in result() method, whenever it comes to else part, I need to get out of the function callthis(). It should not execute kumar() function. Is this possible? Actually, I can use

Solution 1:

There's a bunch wrong here.

First, if (result) just tests whether the variable result contains a truthy value, it doesn't actually invoke the function. If you want to test the return value of the function, you need

if (result()) {

Secondly, you're not understanding that returnimmediately leaves the current function. You can't meaningfully do this:

if(res==true)
{
returntrue;
alert("inside if result");
}

That alert cannot be reached. The function returns immediately when return true is encountered.

Thirdly, to exit callThis early, you simply need to return early. It's up to the function callThis to conditionally return; you cannot force a return from down inside the result function. A function cannot forcibly return out if the context that called it. It's not up to the internals of the result function to determine if kumar should run. You cannot influence the path of execution in the calling method directly. All result can do is return something, or (needlessly complex in this case) accept a callback and conditionally execute it.

Just return from callThis if the result of result() is false:

function calthis()
{
  var i=1;
  if(i==0)
  {
    alert("inside if");
  }
  else
  {
    alert("inside else");
    if (!result()) return;
    kumar();
  }

}

Solution 2:

To exit any function simply use return;

However it seems like you want to call a function if the user clicks confirm, is that correct? If so:

var res = confirm("are you wish to continue");
if (res === true) 
{ 
   kumar(); 
}

If you want to call a function if the user does not click confirm:

var res = confirm("are you wish to continue");
if (!res) 
{ 
   kumar(); 
}

Solution 3:

You got a lot of confusing code going on there, but if the idea is to stop a function, simply use "return false;" and the code execution stops

Post a Comment for "Javascript Confirm Function Ok And Cancel Returns Same Result"