【问题标题】:correct placement of window.setTimeout()正确放置 window.setTimeout()
【发布时间】:2012-03-20 16:55:37
【问题描述】:

我希望定期调用 ajax 函数。下面是我的代码,目前这没有用。如果我正确放置 window.setTimeout() stmnt,任何人都可以提供帮助..

<script>
    var xmlhttp;
    function loadXMLDoc(url, cfunc)
    {
        var config='${config}';
        if (window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=cfunc;
        xmlhttp.open("GET",url+config,true);
        xmlhttp.send();
        }

window.onload=ajaxFunction1();

function ajaxFunction1()
{
    loadXMLDoc("TagDataRetrieval?config=",function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("myDiv1").innerHTML=xmlhttp.responseText;    
        }
    });
    window.setTimeout("ajaxFunction1()", 5000);
}
</script>

【问题讨论】:

  • 您的代码看起来打算每 5 秒调用一次 ajaxFunction1()。那是愿望吗?什么没有按预期工作?
  • 使用cfunc() 调用回调 - 你缺少()
  • 是的,您的代码有什么问题?此外,您可以直接调用 setTimeout(ajaxFunction1,5000) 否,需要评估一个字符串。
  • @Joseph 我认为您在注册 eventHandler 时不需要 ()。
  • 您应该设置不带括号的window.onload = ajaxFunction1;。不知道这是否与问题有关。

标签: javascript html ajax jsp


【解决方案1】:
<script>
    var xmlhttp;
    function loadXMLDoc(url, cfunc)
    {
        var config='${config}';
        if (window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=cfunc;
        xmlhttp.open("GET",url+config,true);
        xmlhttp.send();
        }

function ajaxFunction1()
{
    loadXMLDoc("TagDataRetrieval?config=",function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("myDiv1").innerHTML=xmlhttp.responseText;    
        }
    });
   setTimeout("ajaxFunction1()", 5000);
}
window.onload=ajaxFunction1();
</script>

【讨论】:

  • 请解释为什么您的回答会解决问题。有什么不同?是否只是将window.onload=ajaxFunction1() 移动到最后,因为使用 JavaScript 的函数声明提升应该没有任何区别。
  • 函数没有执行,所以它的调用之后。
  • 通过使用window.onload=ajaxFunction1();末尾的括号,它将立即执行而不是作为事件处理程序,并且结果(在本例中为undefined)将被分配为window.onload跨度>
【解决方案2】:

你可以使用下面的 setTimeout 函数

<script>
    var xmlhttp;
    function loadXMLDoc(url, cfunc)
    {
        var config='${config}';
        if (window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=cfunc;
        xmlhttp.open("GET",url+config,true);
        xmlhttp.send();
        }

function ajaxFunction1()
{
    loadXMLDoc("TagDataRetrieval?config=",function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("myDiv1").innerHTML=xmlhttp.responseText;    
        }
    });

}
setInterval("ajaxFunction1()",5000);
</script>

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 2013-07-05
    • 2013-11-18
    • 1970-01-01
    • 2021-06-29
    • 2017-10-29
    相关资源
    最近更新 更多