AJAX xmlhttprequest 弹出窗口

发布于 2024-11-08 17:44:28 字数 1678 浏览 0 评论 0原文

基本上,我想做的是在页面的一角有一个淡入/淡出弹出窗口。例如,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高跟鞋的旋律 2024-11-15 17:44:28

我将首先使用 jquery 的 'get' 方法,因为您似乎已经在使用 jquery 了。

var old = '';
function checkUpdates() {
    var randomnumber = Math.floor(Math.random() * 10000);
    link = "updates.php?rnd=" + randomnumber;
    $.get(link, function(data) {
        /* called when the resource was successfully retrieved */
        if (old != data) {
            $('#popup').html(data); // set the new data in the popup element
            showpop();              // show the popup
            old = data;             // store the retrieved data
        }
        setTimeout(checkUpdates, 5000);
    });
}

您的 showpop 和 hidepop 函数并未真正正确定义。
也许您可以阅读 document.ready(...)

这应该可以解决问题(请注意,使用 jquery 的 delay 函数,您可以很好地链接所有内容:

function hidepop(){
    $("#popup").fadeOut("slow");
}

function showpop(){
    $("#popup").fadeIn("slow");
    setTimeout(function() {hidepop();}, 4000);
}

I would start by using jquery's 'get' method, since you seem to be using jquery already.

var old = '';
function checkUpdates() {
    var randomnumber = Math.floor(Math.random() * 10000);
    link = "updates.php?rnd=" + randomnumber;
    $.get(link, function(data) {
        /* called when the resource was successfully retrieved */
        if (old != data) {
            $('#popup').html(data); // set the new data in the popup element
            showpop();              // show the popup
            old = data;             // store the retrieved data
        }
        setTimeout(checkUpdates, 5000);
    });
}

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:

function hidepop(){
    $("#popup").fadeOut("slow");
}

function showpop(){
    $("#popup").fadeIn("slow");
    setTimeout(function() {hidepop();}, 4000);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文