开发者

setInterval not working at all?

开发者 https://www.devze.com 2023-03-07 03:27 出处:网络
I have three functions: get_stat(sess) which takes an argument to send to a php script handlestat() which handles the response of the php script

I have three functions: get_stat(sess) which takes an argument to send to a php script handlestat() which handles the response of the php script check_sms(sess_a) which should use setInterval to repeat itself - it relies on a variable disabled_stat to clear the timer

But it's not working, get_stat(sess) is not fired and it just stalls

var disabled_stat = false;
function get_stat(sess)
{
    if(disabled_stat==false)
    {
        var url = "/sms_check_status.php?param=";
        var title_f = document.getElementById('stat_title');
        var stat_f = document.getElementById('stat_text');
        title开发者_如何学C_f.innerHTML = ' ';
        stat_f.innerHTML = ' ';

        var myRandom=parseInt(Math.random()*99999999);
        http.open("GET", url + escape(sess) + "&rand=" + myRandom, true);
        http.onreadystatechange = handlestat;
        http.send(null);
    }
}

function handlestat()
{
    var str_out = '';
    var results = '';
    if (http.readyState == 4)
    {
        results = http.responseText.split("~");
        if(results[0]=='1')
        {
            document.getElementById('stat_title').innerHTML = results[1];
            document.getElementById('stat_text').innerHTML = results[2];
            if(results[3]=='1')
            {
                disabled_stat = true;
            }
        }
    }
}

function check_sms(sess_a)
{
    my_inteval = setInterval("get_stat(sess_a)", 1000);
    if(disabled_stat==true)
    {
        clearInterval(my_inteval);
    }
} 


This line:

my_inteval = setInterval("get_stat(sess_a)", 1000);

won't work, because it's using a string expression, which'll end up evaluated in global scope where the variable sess_a doesn't exist.

Instead, use:

my_inteval = setInterval(function() {
    get_stat(sess_a);
}, 1000);
0

精彩评论

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

关注公众号