IGGt
IGGt

Reputation: 2779

jquery reload page after using ajax to update data

I'm trying to find a way to reload the page after completing an AJAX update.

My Basic code is:

updateEdit = function() {
    $('#editSection').on('click', '#Changes', function() {  
        /*set var here*/
        $.ajaxSetup({ 
            data: {
                var1: 1,
                var2: 2
            }
        })
        ajaxUpdate();   
    });
}

ajaxUpdate = function() {
    $.ajax({
        type: 'GET',
        url: 'update.php',
        data: {},
        success: function(data) {
            alert(data);
            centerItem('#savedBox');
        },
        error: function(jqxhr) {
            alert(jqxhr.responseText);
        }
    })  
}

So far so good. I now need to pause for 1 second, then roload the page. Looking around suggests I need to use setTimeout(location.reload, 1000);

so I added in

complete: function() {
    setTimeout(location.reload, 1000);  
}

to the $.ajaxsetup() but that seemingly did nothing. I then added it to the main ajaxUpdate() function, (not ideal as I don't want it to fire on every ajaxUpdate) whereby I got an Uncaught TypeError: Illegal invocation error. (and no page reload). What am I doing wrong?

Upvotes: 0

Views: 745

Answers (3)

smarber
smarber

Reputation: 5084

updateEdit = function() {
        ...

        // Will wait 1 sec and then reload the page
        var waitCallback = function () {
           setTimeout(function() { location.reload(); }, 1000);
        };

        ajaxUpdate(waitCallback);
    });
}

ajaxUpdate = function(cb) {
    $.get('update.php',
       function(data) {
          alert(data);
          centerItem('#savedBox');

          cb();
        })
        .fail(function(jqxhr) {
            alert(jqxhr.responseText);
        });
};

Upvotes: 0

Karim Seoudy
Karim Seoudy

Reputation: 127

the syntax for setTimeout function is not correct

please use this:

setTimeout(function(){location.reload();}, 1000);

Upvotes: 1

Nathan
Nathan

Reputation: 1520

Reload is a method. Call it via a function

setTimeout(function(){location.reload()}, 1000);

Hope this helps.

Upvotes: 1

Related Questions