Neils Neils - 6 months ago 20
Ajax Question

Database Queries with Ajax without Delay

I am not good in Javascript/jQuery and I have a working AJAX DB Query but unfortunately its taking 3 seconds to appear on Homepage. I want to have it shown immidiately and then it will refresh every 30-60 seconds only.

var autorefresh = setInterval(
function (){
$('#autorefresh').load('eventlog.php').fadeIn('slow');
}, 30000);


the setInterval starts as the pageload is finished and then it takes 30 seconds to appear. Someone can help me?

Thanks!

Answer
  1. Create a new function which holds your logic
  2. Call this function in the load event in jQuery(will execute immediately)
  3. Call the same function in the setInterval block of code(will execute every x milliseconds)

    $(function () {
    
        doStuff();
    
        var autorefresh = setInterval(
        function () {
            doStuff();
        }, 30000);
    
        function doStuff() {
            alert('doStuff() fired...');
            $('#autorefresh').load('eventlog.php').fadeIn('slow');
        }
    });
    
Comments