javascript window.setInterval无法使用最新的Firefox和IE9

时间:2011-10-29 04:50:45

标签: php javascript jquery

我最近为个人需求创建了一个自定义ping box(聊天)。当我编写它并在Firefox 3.6.13中测试时,它工作正常。但是,与window.setInterval相关的功能似乎在IE9或Firefox 6中无法正常工作。

以下是javascript的代码。

    <script>

     function loadNewPosts(){
        var id = $(".altbgcolor:first").attr("id");

        $.get('/updateping.php', { updateid: id ,  }, function(data){
                                $(".newslist").prepend(data);
                            }, 'html');
     }

     window.setInterval(loadNewPosts, 1000*3)   


     $(document).ready(function() { 
    // bind form using ajaxForm 

             $("#pingForm").validate({


                submitHandler: function(form) {  
                $('#pingForm').ajaxSubmit({ 
                        // target identifies the element(s) to update with the server response 
                        target: '#pingResult', 

                        // success identifies the function to invoke when the server response 
                        // has been received; here we apply a fade-in effect to the new content 
                        success: function() { 

                            $('#msg').val('');
                            $('#pingResult').fadeIn('slow'); 
                            $('#pingResult').fadeOut(2000); 
                        } 
                    });
                }
            }); 

        })


 </script> 

下面是HTML

      <ul class="newslist" style="width:630px;">

        <li class="altbgcolor" id=64>
    <div>   
        <div class="newsthumb" style="width:50; height:50; "center center no-repeat;"><img src="images/personal/sunny.jpg" /></div>



        <div class="newstext" style="margin:0px;">


        <h1 style="color:#081C28;"><img width="11" height="9" src="/content/icon_topic_newest.gif">how r u?</h1>

        </div>
        <br /><br />
        <div style="font-size: 0.6em; color:#666666;">  
            <span style="text-decoration:none; color:none; padding:5px;"><i> from: <a href="" style="text-decoration: none;color:#105289 ;" onmouseover="this.style.textDecoration = 'underline'" onmouseout="this.style.textDecoration = 'none'">Sunny</a></i></span>                          
            <span style="text-decoration:none; color:none; padding:5px; "><i> posted: <a href="" style="text-decoration: none;color:#105289 ;" onmouseover="this.style.textDecoration = 'underline'" onmouseout="this.style.textDecoration = 'none'">October 29, 2011, 9:58 am</a></i></span>                           
        </div>  

        <div class="clear"></div>
     </div>


    </li>
        </ul>

我尝试使用Firebug调试问题,但是看起来似乎window.setInterval能够每隔3秒调用带有所需参数的.php文件,但是它不会显示php文件的o / p。

php代码(updateping.php)

        <?

        require_once('includes/connection.php');
        require_once('includes/functions.php');

        if(isset($_GET['updateid']))
        {
            $updateid=$_GET['updateid'];
            $sql="SELECT * FROM ping WHERE id > $updateid ORDER BY id DESC";

            $res=mysql_query($sql,$connection);

            if(@mysql_num_rows($res))
            {
                while($data=mysql_fetch_array($res))
                        //while($data=mysql_fetch_array($result))
                        {
                                echo '<li class="altbgcolor" id='.$data['id'].'>
                                    <div>   
                                        <div class="newsthumb" style="width:50; height:50; center center no-repeat;"><img src="'.$data['img'].'"  /></div>



                                        <div class="newstext" style="margin:0px;">


                                        <h1 style="color:#081C28;"><img width="11" height="9" src="/content/icon_topic_newest.gif">'.stripslashes($data['msg']).'</h1>

                                        </div>
                                        <br /><br />
                                        <div style="font-size: 0.6em; color:#666666;">  
                                            <span style="text-decoration:none; color:none; padding:5px;"><i> from: <a href="" style="text-decoration: none;color:#105289 ;" onmouseover="this.style.textDecoration = \'underline\'" onmouseout="this.style.textDecoration = \'none\'">'.$data['name'].'</a></i></span>                          
                                            <span style="text-decoration:none; color:none; padding:5px; "><i> posted: <a href="" style="text-decoration: none;color:#105289 ;" onmouseover="this.style.textDecoration = \'underline\'" onmouseout="this.style.textDecoration = \'none\'">'.$data['entry_date'].'</a></i></span>                         
                                        </div>  
                                        <div class="clear"></div>
                                     </div>


                                    </li>'; 


                        }


            }

        }




        ?>

我在这里修复,不明白可能是什么问题。该聊天框在FF 3.6.13

上工作正常

请帮帮我一个人!!!!!!!!!!!

更新 我尝试了IE9'F12'开发人员调试器,发现每隔3秒就认为php文件被称为返回结果类型304-Not modified。 :((

我在调试器菜单上找到了“缓存”选项,并选中了“始终从服务器刷新”选项和bammmm !!!!它现在有效。

任何想法如何在实际的IE和Firefox设置中调整它。问题似乎只与缓存有关。

2 个答案:

答案 0 :(得分:3)

由于你已经在使用jQuery,你可以试试这个:

$(document).ready(function() {
  $.ajaxSetup({ cache: false });
});

根据jQuery .ajax,doco jQuery会自动为您的请求添加时间戳,以便浏览器不会尝试将缓存用于后续请求。

或者您可以尝试使用HTTP响应标头的“缓存控制”部分中的设置修复服务器端的缓存问题,但我不知道如何在PHP中执行此操作。

答案 1 :(得分:2)

为获取请求添加时间戳,或者使用post。

$.get('/updateping.php', { updateid: id , time: new Date().valueOf() }, function(data){
    ...

或者

$.post('/updateping.php', { updateid: id }, function(data){
    ...