AJAX xmlhttprequest 弹出窗口
基本上,我想做的是在页面的一角有一个淡入/淡出弹出窗口。例如,John Doe 刚刚上线。我正在使用 AJAX 检查最新更新,如果是新的,则显示弹出窗口。
显示弹出窗口的函数(使用 jquery)是:
function hidepop(){
$("#popup").fadeOut("slow");
}
function showpop(){
$("#popup").fadeIn("slow");
setTimeout("hidepop()",4000);
}
更新和显示弹出窗口的代码(如有必要)是:
function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX. Please download Google Chrome or Firefox.");
return null;
}
}
function checkUpdates(old){
httpObject = getHTTPObject();
var randomnumber=Math.floor(Math.random()*10000);
if (httpObject != null) {
link = "updates.php?rnd="+randomnumber;
httpObject.open("GET", link , true);
httpObject.onreadystatechange = function() {
if(httpObject.readyState == 4){
var response = httpObject.responseText;
var objDiv = document.getElementById("popup");
objDiv.innerHTML = response;
if(response == old){
var time = setTimeout(function(){checkUpdates(response); response = null},5000);
}else{
var objDiv = document.getElementById("popup");
objDiv.innerHTML = response;
showpop();
var time = setTimeout(function(){checkUpdates(response); response = null},5000);
}
}
}
}
}
然后,开始一切:
onload="checkUpdates('');"
在 body 标记上。
所以,我的问题是……什么也没发生。我知道显示弹出窗口是有效的,因为如果我调用 showpop() 它就会显示。
这可能是一些愚蠢的错误,但是,您能给我一些关于问题可能是什么以及如何解决它的想法/指示吗?
非常感谢, 卡勒姆。
Basically, what I'm trying to do is have a fade in/out popup in the corner of my page. e.g. John Doe has just come online. I'm using AJAX to check for the latest updates, if they are new, display the popup.
the function to display popup (using jquery) is:
function hidepop(){
$("#popup").fadeOut("slow");
}
function showpop(){
$("#popup").fadeIn("slow");
setTimeout("hidepop()",4000);
}
The code to update and show the popup, if necessary, is:
function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX. Please download Google Chrome or Firefox.");
return null;
}
}
function checkUpdates(old){
httpObject = getHTTPObject();
var randomnumber=Math.floor(Math.random()*10000);
if (httpObject != null) {
link = "updates.php?rnd="+randomnumber;
httpObject.open("GET", link , true);
httpObject.onreadystatechange = function() {
if(httpObject.readyState == 4){
var response = httpObject.responseText;
var objDiv = document.getElementById("popup");
objDiv.innerHTML = response;
if(response == old){
var time = setTimeout(function(){checkUpdates(response); response = null},5000);
}else{
var objDiv = document.getElementById("popup");
objDiv.innerHTML = response;
showpop();
var time = setTimeout(function(){checkUpdates(response); response = null},5000);
}
}
}
}
}
Then, to start it all off:
onload="checkUpdates('');"
on the body tag.
So, my problem is... nothing happens. I know that showing the popup works because if I call showpop() it will show.
This is probably some stupid mistake but, could you please give me some ideas/pointers to what the problem might be and how to fix it.
Thank you very much,
Calum.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将首先使用 jquery 的 'get' 方法,因为您似乎已经在使用 jquery 了。
您的 showpop 和 hidepop 函数并未真正正确定义。
也许您可以阅读 document.ready(...)。
这应该可以解决问题(请注意,使用 jquery 的 delay 函数,您可以很好地链接所有内容:
I would start by using jquery's 'get' method, since you seem to be using jquery already.
Your showpop and hidepop functions are not really defined correct.
Maybe you could read up on document.ready(...).
This should do the trick (notice that with jquery's delay function you can nicely chain everything: