admin管理员组文章数量:1392099
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_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);
}
}
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_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);
}
}
Share
Improve this question
edited May 20, 2011 at 17:48
Mike Samuel
121k30 gold badges227 silver badges254 bronze badges
asked May 20, 2011 at 17:42
Andy GeeAndy Gee
3,3522 gold badges33 silver badges48 bronze badges
1 Answer
Reset to default 9This 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);
本文标签: javascriptsetInterval not working at allStack Overflow
版权声明:本文标题:javascript - setInterval not working at all? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744780493a2624681.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论