Reputation: 2779
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
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
Reputation: 127
the syntax for setTimeout function is not correct
please use this:
setTimeout(function(){location.reload();}, 1000);
Upvotes: 1
Reputation: 1520
Reload is a method. Call it via a function
setTimeout(function(){location.reload()}, 1000);
Hope this helps.
Upvotes: 1