Skip to content Skip to sidebar Skip to footer

Ajax Jquery Update Page Without Refreshing

I currently have the below function which updates the data in a div when the page is refreshed and this works fine however i want to edit the function to make it constantly update

Solution 1:

You can chain setTimeout calls to achieve this:

$(document).ready(function() {
    functionupdateOrders() {
        $.ajax({                                      
            url: 'getOrderStatus.php',                                  
            dataType: 'json',
            success: functionajaxLoop(data) {
                varOrdersSubmitted = data[0].SUBMITTED;
                varOrdersFulfilled = data[0].FULFILLED;
                $('#OrdersSubmitted').html("SUBMITTED:"+ OrdersSubmitted);
                $('#OrdersFulfilled').html("FULFILLED:"+ OrdersFulfilled);

                setTimeout(updateOrders, 2000);
            } 
        });
    });

The alternative is setInterval(), however if the requests slow down this can lead to calls being queued, which will eventually lead to memory issues.

Solution 2:

You need to add a repeating event to call your updateOrders function. Like:

function startUpdateOrdersTimes() {
    setInterval(function() {
        updateOrders();
    }, 2000);
    //Call now (otherwise waits for first call)updateOrders();
}

Solution 3:

Using "window.setInterval" (https://developer.mozilla.org/en/docs/Web/API/window.setInterval) you can repeatedly execute a function at a specified time interval.

functionSomeFunction()
{
    $.ajax({...});
}

window.setInterval(SomeFunction,2000);

This would execute SomeFunction every 2 seconds

Hope this helps

Solution 4:

timerupdateorders = setInterval(function() {
    ajaxLoop();
}, 2000);

You may use

clearInterval(timerupdateorders);

to end the timer

Post a Comment for "Ajax Jquery Update Page Without Refreshing"