簡體   English   中英

javascript window.setInterval無法在最新的Firefox和IE9上運行

[英]javascript window.setInterval not working on latest Firefox and IE9

我最近創建了一個自定義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-未修改。 :((

我轉到調試器菜單上的“緩存”選項,並選中了“始終從服務器刷新”選項和bammmm! 它現在工作了。

任何想法如何在實際的IE和Firefox設置中進行調整。 該問題似乎僅與緩存有關。

由於您已經在使用jQuery,因此可以嘗試以下操作:

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

按照jQuery .ajax doco,jQuery會自動將時間戳添加到您的請求中,以使瀏覽器不會嘗試將緩存用於后續請求。

或者,您可以嘗試使用HTTP響應標頭的“緩存控制”部分中的設置來修復服務器端的緩存問題,但是我不知道如何在PHP中做到這一點。

在您的get請求中添加時間戳,或者改用post。

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

要么

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM