AJAX jQuery refresh div every 5 seconds

前端 未结 5 505
忘了有多久
忘了有多久 2020-11-27 05:02

I got this code from a website which I have modified to my needs:




        
相关标签:
5条回答
  • 2020-11-27 05:53

    Try to not use setInterval.
    You can resend request to server after successful response with timeout.
    jQuery:

    sendRequest(); //call function

    function sendRequest(){
        $.ajax({
            url: "test.php",
            success: 
            function(result){
                $('#links').text(result); //insert text of test.php into your div
                setTimeout(function(){
                    sendRequest(); //this will send request again and again;
                }, 5000);
            }
        });
    }
    
    0 讨论(0)
  • 2020-11-27 05:57

    Try this out.

    function loadlink(){
        $('#links').load('test.php',function () {
             $(this).unwrap();
        });
    }
    
    loadlink(); // This will run on page load
    setInterval(function(){
        loadlink() // this will run after every 5 seconds
    }, 5000);
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-27 05:58
    <script type="text/javascript">
    $(document).ready(function(){
      refreshTable();
    });
    
    function refreshTable(){
        $('#tableHolder').load('getTable.php', function(){
           setTimeout(refreshTable, 5000);
        });
    }
    </script>
    
    0 讨论(0)
  • 2020-11-27 06:01

    Try using setInterval and include jquery library and just try removing unwrap()

    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
    
    var timeout = setInterval(reloadChat, 5000);    
    function reloadChat () {
    
         $('#links').load('test.php');
    }
    </script>
    

    UPDATE

    you are using a jquery old version so include the latest jquery version

    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    
    0 讨论(0)
  • 2020-11-27 06:06

    you can use this one.

    <div id="test"></div>
    

    you java script code should be like that.

    setInterval(function(){
          $('#test').load('test.php');
     },5000);
    
    0 讨论(0)
提交回复
热议问题