javascript – 长轮询jQuery在IE中不起作用

$.ajax似乎在IE中不起作用.我该怎么做或者它只是IE中的另一个错误?我是否需要在此处提供代码才能获得帮助?因为它似乎不适用于任何$.ajax示例.

我的代码

function get_info(lines) {
    $.ajax({
        type: "POST",
        cache: false,
        url:  "chat.PHP?RandomNumber=" + Math.random(),
        data: "type=get_info&lines="+lines+"&RandomNumber=" + Math.random(),
        dataType: "json",
        success: function(msg){
          lines = msg.lines;

          if(msg.new_messages)
          {
            for(i = 0; i < msg.messages.length; i++)
            {
                $('.chat').append("<p>"+msg.messages[i]+"</p>");      
            }
            document.getElementById('chatty').scrollTop = document.getElementById('chatty').scrollHeight;
          }
        },
        complete: function() {
            setTimeout(get_info, 1000, lines); 
        }
    })    

};    

setTimeout(get_info, 1000, 0); 

解决方法:

我现在看到你正在使用一种不能与IE1,2一起使用的setTimeout形式:

setTimeout(myFunction,myTimeout,parameter); //does NOT work for IE

相反,使用匿名函数作为参数,该参数应使用正确的参数调用目标函数

setTimeout(function(){myFunction(myParameter);},myTimeout);

因此,您对setTimeout的初始调用应更改为:

setTimeout(function(){get_info(0);}, 1000); 

随后的成功呼吁应该是:

setTimeout(function(){get_info(lines);}, 1000); 

如果这是因为IE正在缓存你的GET请求,你可以简单地为jQuery.ajax()设置cache为false,让jQuery为你处理它(记得在进行这个更改后清除你的缓存):

//do this for *all* ajax requests
$.ajaxSetup ({
    cache: false
});

要么

//do it for this ajax request
$.ajax ({
    cache: false,
    //..other options here
});

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...