简体   繁体   English

IE8问题与javascript函数在setTimeout中调用自身

[英]IE8 problem with javascript function that calls itself in setTimeout

i have a locate function defined in javascript 我有一个在JavaScript中定义的定位函数

var locID;

function locateMe()
{
    if(locID > 0)
    {
        // i do a jquery post here
    }

    setTimeout(locateMe, 2000);
} 

// my document ready function is here, and inside it, at the end of it
// i do this
locID = 0;
locateMe();

when i test this code in firefox, the locateMe function is called every two seconds and works as expected. 当我在firefox中测试此代码时,每两秒调用一次locateMe函数并按预期工作。 when i test the code in IE8 the function is never called (at least it appears to never be called from what i can see using IE's developer tools) 当我在IE8中测试代码时,函数永远不会被调用(至少它看起来永远不会被我使用IE的开发人员工具看到)

note: there is code defined in a click event handler for the 'zone_row' class that modifies locID. 注意:在“zone_row”类的click事件处理程序中定义了修改locID的代码。 again, in firefox everything works as expected. 再次,在Firefox中一切都按预期工作。 the strange thing is, in IE when a zone_row is clicked the function WILL be called ONCE. 奇怪的是,在IE中点击zone_row时,函数将被称为ONCE。 i can see that both on the developer tools and through the result of the action of that jquery post. 我可以看到开发人员工具和jquery帖子的操作结果。

i figured there is just some anomly with IE that i am not familiar with yet. 我认为IE中只有一些我不熟悉的异常情况。 what am i doing wrong? 我究竟做错了什么?

EDIT: changed "locateMe();" 编辑:更改“locateMe();” to locateMe inside the setTimeout call. 在setTimeout调用中定位Me。

UPDATE: adding more of my code (per request in comments) to show placement (albeit not much more code than my first post). 更新:添加更多我的代码(在评论中的每个请求)以显示位置(尽管代码不比我的第一篇文章多)。

<script type="text/javascript">
    var z_items;
    var locID;

    function locateMe()
    {
            if(locID > 0)
            {
                    // my jquery post is here                   
            }   

            setTimeout(locateMe, 2000);
    }

    $(document).ready(function()
    {
            // ... some click events and get requests here ...

            locID = 0;
            locateMe();
    });
</script>

i also tried wrapping the call in a setTimeout (no effect) and changing the DOCTYPE (this actually caused IE to never call the function as opposed to now where it calls it ONCE and never again). 我也尝试在setTimeout中包装调用(没有效果)并更改DOCTYPE(这实际上导致IE从不调用函数,而不是现在它称之为ONCE并且再也不会)。

problem solved. 问题解决了。 i found an answer to another problem i was having from this post: 我找到了这个帖子中我遇到的另一个问题的答案:

Prevent browser caching of jQuery AJAX call result 防止浏览器缓存jQuery AJAX调用结果

upon adding $.ajaxSetup({ cache: false }); 添加$ .ajaxSetup({cache:false}); to my document ready function, it solved THIS problem too. 对于我的文档就绪功能,它也解决了这个问题。 it looks like all this time it was a caching issue. 它看起来像是一个缓存问题。

I've found that for IE (even IE9) that if you nest the self-called function in an anonymous function it works. 我发现,对于IE(甚至是IE9),如果你将自调用函数嵌套在一个匿名函数中,它就可以工作。 But it does look like Toddeman's problem was related to the ajax part. 但看起来Toddeman的问题确实与ajax部分有关。

So the code would be: 所以代码是:

function locateMe()
{
    /* ... */   

    //IE way (still works in Chrome and FF):
    setTimeout(function () { locateMe(); }, 2000);

    //original: setTimeout(locateMe, 2000);
}

使用

setTimeout( locateMe, 2000 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM