Using Global variables in JavaScript while using Recursive function

Hi,

I have a function that is called recursively in JavaScript as below. (In VB.NET front end)
I want to set a global variable so that it returns or EXITS from the recursion when that code is executed.
Also the global variable makes sure that the code is not execute again.

How to do this as the GLOBAL variable accessed inside the recursion is re-initialized !!!!!



var FireStatusGlobalVal = 0;

function FireNotification(param1, param2)
{
    ....
    ....
         param1 = ........;
         param2 = ........;

         if(FireStatusGlobalVal <= 0)   //Run the conditional code
         {
                  if(param1 == param2)   //Some condition
                  {
                           //run an activity
                           FireStatusGlobalVal = 1;
                  }
         }
         else
         {
                  return true;
         }
    ....
    ....
    FireNotification(param1, param2);  //CALL RECURSIVELY
}
ArikkanAsked:
Who is Participating?
 
skijCommented:
Try:
window.stopFireNotification

Are you sure it is loosing scope?
0
 
skijCommented:
var stopFireNotification = false;
function FireNotification(param1, param2)
{
    if(stopFireNotification) return false;
    /// ...

Open in new window

When stopFireNotification is set to true, that function won't run.
var stopFireNotification = true;
0
 
Tom BeckCommented:
Or how about a while lop?
function FireNotification(param1, param2)
{
   while ((param1 != param2) {
      FireNotification(param1, param2);  //CALL RECURSIVELY while CHANGING param1 and/or param2
   }
}

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
ArikkanAuthor Commented:
But that is the problem.
Once I get out of the scope of the Recursive function, the Global variable does not seem to retain its value.

So this code will never run in the outer scope:

if(stopFireNotification) return false;
0
 
skijCommented:
I understand the problem now.

Anything you add to "window" will be global in scope, for example:
window.stopFireNotification = false;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example</title>
</head>
<body>

</head>
<body>
<h1>Hello</h1>


<script type="text/javascript">

window.stopFireNotification = false;

setTimeout(function(){ window.stopFireNotification = true }, 10000); 

function FireNotification(param1, param2)
{
    if(window.stopFireNotification) return false;
    alert(window.stopFireNotification);
    setTimeout(function(){ FireNotification(param1, param2) }, 3000); 
}

FireNotification("param1", "param2");

</script>
<body>


</body>
</html>

Open in new window

0
 
ArikkanAuthor Commented:
It did not help. I changed the variable used to "window.stopFireNotification" as you suggested.

Still it loses the value in outer scope (After the return from recursive call) and resets to 'false' or '0'.

Any ideas?
0
 
ArikkanAuthor Commented:
I tried.

Is there any other method
0
 
Brian TaoSenior Business Solutions ConsultantCommented:
If you can post the actual function instead of the pseudo code, we might be able to help.
0
 
ArikkanAuthor Commented:
------------------------------------------------------------------------------------------------------------------------------------------------------
Code Behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    ....
    ....
    ....
        AddJavaScriptAttribute()
    ....
    ....
End Sub

Private Sub AddJavaScriptAttribute()
             ScriptManager.RegisterStartupScript(Page, Page.GetType(), "FireNotification", "FireNotification('" & Me.divError.ClientID() & "','" & Me.divSuccess.ClientID() & "','myCookie','" & Me.hdnMessage.ClientID() & "','" &  DirectCast(wizCandidate.FindControl("FinishNavigationTemplateContainerID$btnPay"), Button).ClientID() & "','" & DirectCast(wizCandidate.FindControl("FinishNavigationTemplateContainerID$btnFinish"), Button).ClientID() & "','" & DirectCast(wizCandidate.FindControl("FinishNavigationTemplateContainerID$StepPreviousButton"), Button).ClientID() & "','" & DirectCast(wizCandidate.FindControl("FinishNavigationTemplateContainerID$hiddenFieldbtnFinishClicked"), HiddenField).ClientID() & "','false');", True)

End Sub

------------------------------------------------------------------------------------------------------------------------------------------------------
JavaScript for ASPX Page:

window.stopFireNotification = 0;

function FireNotification(divError, divSuccess, myCookie, hdnMessage, btnPay, btnFinish, btnPrevious, hiddenFieldbtnFinishClicked, FirstTime)
{
    if (window.stopFireNotification > 0)   //The Global Variable is not updated after return from recursive function
    {
        return false;
    }
    else
    {
        if (FirstTime == 'true')  {  eraseCookie(myCookie, ' ', -1);  }

        var cookieValue = readCookie(myCookie);
        if (cookieValue != undefined)
        {
                if (cookieValue == 'success')
                {
                  var btnFinishOnline = document.getElementById(btnFinish);
                  btnFinishOnline.disabled = false;              
                  window.stopFireNotification = 1;
                  return true;
                }
        }
       else
       {
            setTimeout("FireNotification('" + divError + "','" + divSuccess + "','" + myCookie + "','" + hdnMessage + "','" + btnPay + "','" + btnFinish + "','" + btnPrevious + "','" + hiddenFieldbtnFinishClicked + "','" + FirstTime + "')", 1000);
       }
    }
}
------------------------------------------------------------------------------------------------------------------------------------------------------


Any ideas guys?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.