php - javascript window.setInterval 不适用于最新的 Firefox 和 IE9

标签 php javascript jquery

我最近根据个人需要创建了一个自定义 ping 框(聊天)。当我对它进行编码并在 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 设置中对此进行调整。该问题似乎仅与缓存有关。

最佳答案

既然你已经在使用 jQuery,你可以试试这个:

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

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

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

关于php - javascript window.setInterval 不适用于最新的 Firefox 和 IE9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7937081/

相关文章:

PHP:在将 SQL 处理到 Mysql 之前检查 SQL 的有效性

php - Symfony2 : Edit user without having password

javascript - 如何并排放置 ReactJS-bootstrap 表单和 h1 标签

javascript - jQuery 和 Prototype 方法相互干扰

javascript - 如何获取具有属性 tabindex 的下一个元素

jquery - Bootstrap navbar-fixed-top 不工作

php - 通过php发送html表单数据到sql数据库(使用mysqli)

PHP - 回显 MySQL SELECT 查询的所有结果

javascript 将数组合并为单个对象

javascript - 在 Gmail Javascript 中检测互联网连接