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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
skijCommented:
Try:
window.stopFireNotification

Are you sure it is loosing scope?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.