为什么这个 setTimeout 不起作用?里面的相关问题

发布于 2024-12-06 21:07:39 字数 1126 浏览 2 评论 0原文

我的页面上有这个脚本,并且 setTimeout 函数永远不会触发。现在这只是一个警报,但我只是在测试它。如果有任何线索的话,我会在页面上进行元刷新,但我也给出了 10 秒的延迟,以便页面在应该触发之前不会刷新。

另外,相关问题:如果我运行一个延迟 10 秒(使用 setTimeout)的 javascript,并且在该 javascript 中,我尝试修改一个设计元素,该元素在声明 setTimeout 时不在页面上,但将由脚本被触发的时间。它会起作用吗?

<script language=javascript>

var xmlhttp_get_memento;    
function loop_alerte(){
    setTimeout( function() {
        alert("timeout");
    }, 5000);
    xmlhttp_get_memento = new XMLHttpRequest();
    if (xmlhttp_get_memento==null)
    {
        alert ("Browser does not support HTTP Request (1)");
        return;
    }   
    var url="crm/ajax/get_mementos.php";
    url=url+"?sid="+Math.random();
    xmlhttp_get_memento.onreadystatechange=function() {
        if (xmlhttp_get_memento.readyState == 4) {  
            alert(xmlhttp_get_memento.responseText);                                        
            schimbare_tip_cursor("default");
        }
        else{
            schimbare_tip_cursor("progress");
        }
    };

xmlhttp_get_memento.open("GET",url,true);
xmlhttp_get_memento.send(null);
}

loop_alerte();
</script>';

I have this script on a page of mine and the setTimeout function never fires. It's just an alert right now but i'm just testing it out. I'm doing a meta refresh on the page just after it if that's any clue, but i've also given that a 10 sec delay so the page isn't refreshed before it's supposed to trigger.

Also, the related question: If I run a javascript with a delay of, say, 10 seconds (with setTimeout) and in that javascript I try to modify a design element that's not on the page when the setTimeout is declared but will be by the time the script is fired. Will it work?

<script language=javascript>

var xmlhttp_get_memento;    
function loop_alerte(){
    setTimeout( function() {
        alert("timeout");
    }, 5000);
    xmlhttp_get_memento = new XMLHttpRequest();
    if (xmlhttp_get_memento==null)
    {
        alert ("Browser does not support HTTP Request (1)");
        return;
    }   
    var url="crm/ajax/get_mementos.php";
    url=url+"?sid="+Math.random();
    xmlhttp_get_memento.onreadystatechange=function() {
        if (xmlhttp_get_memento.readyState == 4) {  
            alert(xmlhttp_get_memento.responseText);                                        
            schimbare_tip_cursor("default");
        }
        else{
            schimbare_tip_cursor("progress");
        }
    };

xmlhttp_get_memento.open("GET",url,true);
xmlhttp_get_memento.send(null);
}

loop_alerte();
</script>';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

惯饮孤独 2024-12-13 21:07:39

您的 setTimeout 看起来不错,所以可能还有其他问题。您是否尝试过使用 javascript 调试器来查看是否出现任何错误?

至于你的第二个问题,是的,这不应该有任何问题,因为 setTimout 内的匿名函数在运行之前不会被评估。此处的实时示例:http://jsbin.com/afonup/2/edit 有和没有jQuery。

Your setTimeout looks good, so there's probably something else that's wrong. Have you tried using a javascript debugger to see if you get any errors?

As for your second question, yes, that shouldn't be any problem, as the anonymous function inside the setTimout won't be evaluated until it runs. Live sample here: http://jsbin.com/afonup/2/edit Both with and without jQuery.

青春有你 2024-12-13 21:07:39

你的setTimeout没有问题,你需要进一步调试
至于你的第二个问题——该函数将运行,但无论你试图做什么都不会起作用。

There is nothing wrong with your setTimeout, you will need to debug further
As for your second question -- the function will run, but whatever it is you were trying to do will not work.

寒尘 2024-12-13 21:07:39

清理你的代码将是一个很好的开始。我可以想象浏览器不理解标签

Cleaning up your code would be a nice start. I can imagine a browser doesn't understand the tag <script language=javascript>. I suggest to use <script type="text/javascript"> and if you're lucky, your javascript might work!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文