AJAX jQuery refresh div every 5 seconds

前端 未结 5 521
忘了有多久
忘了有多久 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);
            }
        });
    }
    

提交回复
热议问题