Reputation: 243
<a href="newpage.php" target="_blank" onClick="openWindowReload(this)">update</a>
<script>
function openWindowReload(link) {
var href = link.href;
window.open(href,'_blank');
document.location.reload(true)
}
</script>
so i have this code that automatically refreshing the current page after clicking the link 'update'
what i want is to refresh the current page after 5 secs....why?
in the newpage.php i am updating a table and if the current page reload very fast it will not get the data that is updated, instead it will get the old data...ps sorry for my bad english. . . .
is there any way that i can pause javascript that will refresh the current page..if there is can u guys tell me how....
thanks in advance....
update:
<script>
setTimeout(function openWindowReload(link) {
var href = link.href;
window.open(href,'_blank');
document.location.reload(true)
}, 5000);
</script>
i tried to use time out but its not working.....
Upvotes: 0
Views: 3119
Reputation: 31
<button type="button" name="update" class="update">Update</button>
$(".update").on('click', function(event){
setTimeout(function() {
document.location.reload(true);
}, 5000); //5000 means 5 second After 5 second page is autmatically refresh
});
For More Description Check Demo
Upvotes: 0
Reputation: 20842
For what you are trying to accomplish, I would use an AJAX call to the server to do the table update, and assuming the update is synchronous (your service waits until update is complete before returning), you then update the client content or refresh the page from the callback event handler. That way you dont have to wait an arbitrary time, plus if you have decided you need 5sec, then there may be times when even 5sec wont be enough.
The root of the problem seems to be a race condition from not simply querying the table from the same connection that did the update. Most updates finish in less than a second, you could just block the initial page while you do both operations in serial fashion, and avoid the race condition.
Upvotes: 0
Reputation: 12127
you are trying to open window by javascript
code, due to security reason, the dynamic popup/window would be block until you are not allowing to open popup.
the code will be work fine if you allow popup on browser
<a href="newpage.php" target="_blank" onClick="return openWindowReload(this)">update</a>
<script>
function openWindowReload(link) {
setTimeout(function(){
var href = link.href;
window.open(href,'_blank');
document.location.reload(true)
}, 5000);
return false;
}
</script>
Upvotes: 0
Reputation: 15213
You could use setTimeout
:
setTimeout(function() {
document.location.reload(true);
}, 5000);
That is a way of 'pausing' JavaScript, or better, waiting to execute a bit of functionality.
So it would be:
function openWindowReload(link) {
var href = link.href;
window.open(href,'_blank');
setTimeout(function() {
document.location.reload(true);
}, 5000);
}
Upvotes: 3