setInterval 根本不起作用?
我有三个功能: get_stat(sess) 接受一个参数发送到 php 脚本 handlestat() 处理 php 脚本的响应 check_sms(sess_a) 应该使用 setInterval 来重复自身 - 它依赖于变量disabled_stat来清除计时器
但是它不起作用, get_stat(sess) 没有被触发并且它只是停止
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此行:
不起作用,因为它使用字符串表达式,最终将在变量
sess_a
不存在的全局范围内进行计算。相反,使用:
This line:
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: