开发者

One function is not defined when trying to do two function recursion

开发者 https://www.devze.com 2023-03-28 18:51 出处:网络
I have a page which clients sometimes leave open for extended periods of time without refreshing (over 24hrs).Some of the actions on that page require valid PHP session so I have built a simple set of

I have a page which clients sometimes leave open for extended periods of time without refreshing (over 24hrs). Some of the actions on that page require valid PHP session so I have built a simple set of functions to run this check every 10 minutes.

2 Functions:

  1. checkLogin()
  2. refreshTimer()

We call checkLogin() to start, checkLogin() calls refreshTimer(). After timer completes it should call checkLogin(), which should call refreshTimer() and start the process all over again.

The first time we call checkLogin() directly things works great. However, when refreshTimer() tries开发者_Python百科 to call checkLogin() I get a "function not defined" error for checkLogin().

From my research it looks like I should be able to call the checkLogin() without passing it to the refreshTimer().

I'm sure I'm probably missing something obvious. Thanks for your help!

function checkLogin()
{
    $.ajax({
        url: "/mysite.com/dev/includes/check_login_helper.php",
        success: function(logged_in)
        {
            if(logged_in == "false")
            {
                // they are logged out - redirect them to login page
                alert("logged out");
            }
        }
    });
    refreshTimer();
}


function refreshTimer()
{
    var t = setTimeout("checkLogin()",15000); // do each 10 min currently shorter for test
}

//start the process up
checkLogin();

Fixed & using checkInterval

function checkLogin()
{
    $.ajax({
        url: "/mysite.com/dev/includes/check_login_helper.php",
        success: function(logged_in)
        {
            if(logged_in == "false")
            {
                // they are logged out - redirect them to login page
                alert("logged out");
            }
        }
    });
}


  setInterval(checkLogin(),15000); // do each 10 min currently shorter for test


Don't pass a string to setTimeout; it'seval in disguise. There is also no reason to capture the value returned by setTimeout in this case.

function refreshTimer()
{
    setTimeout(checkLogin, 15000);
}


var t = setTimeout("checkLogin()",15000);

needs to be

var t = setTimeout(checkLogin,15000);

this method accepts functions and not strings.

0

精彩评论

暂无评论...
验证码 换一张
取 消