Refresh content automatically after some period time – jQuery
If you just need to refresh your page or content every x seconds you just need some javascript. I show you some basics with jQuery.
Most important method
setInterval(function,milisec,lang);
It’s javascript primary method which allow us to call some function every x miliseconds. You can read more about it at w3schools.com
I’ll be using it in all my examples. First two parameters are required, while third is optional. Remember that 1s = 1000ms.
For example if I want to call function my_function() every 5 seconds (5 seconds * 1000 = 5000ms) I just write it like this:
setInterval("my_function();",5000);
Refresh whole page
Sometimes we need to refresh whole page. It’s not so common now because it’s timeconsuming and bad for performance, but it needs to be mention. So to refresh page every 5 seconds our code would look like that:
<script type="text/javascript">
setInterval("my_function();",10000);
function my_function(){
window.location = location.href;
}
</script>
Refresh part of the page
If you need to refresh content in some div of the page you can use jQuery load() function.
$('#id_for_refresh').load('your_url #id_of_loaded_content');
For better understanding see example below:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js">
<script type="text/javascript">
setInterval("my_function();",5000);
function my_function(){
$('#refresh').load(location.href + ' #time');
}
</script>
</head>
<body>
<div id="refresh"></div>
<div id="time">
<?php echo date('H:i:s');?>
</div>
</body>
</html>
You can see that every 5 sec div with id=”refresh” is updated. Of course our div in html can look like this
<div id="refresh">
<div id="time">
<?php echo date('H:i:s');?>
</div>
</div>
i used two separate containers so you can see the changes. You can also specify the url for other page, or send some data to it. But this is used more with interaction with user and not with auto refreshing.
If you also want to see something more complicated, check Volume II of ajax auto refresh with working example.
AMAZING!!!!
СПАСИБО :)
thank you so much
Thank you very much.
I give something for your website have text php code, use html special chars, because i look > at the code not “<" , good tutorial :D
Kokars very nice tutorial,
Is there any way where i refresh the table data automatically ? and it will chk with current server time.
useful, giving a lot good ideas..works in firefox..i`m also recommended document.
Thanks Veere
wow !!! great… thankx man
dont work for me
Nice Post.Thank You.
Thank You……
what if i want to load a video should i do the same?
what if i want to load a video should i do the same?
not working
Thank you for this. I do however have a question, I have surfed the whole web and can not find a working answer. How do I get to stop the refresh? If I want to submit content the page keeps reloading, with empty fields… Very frustrating!
Thanks in advance
kokers very nice tutorial and thanx for sharing
2 days of trying to find the best solution to refresh a <?php echo $this->getRandomImage() ?> inside the div.. Your's is the first one which worked! YOU ARE A LEGEND!
yuppiee..its working…thanks..
grzes,
przygotuj dedykowany url do pobrania samych komentarzy. Zamiast odświeżać całą stronę po prostu zrób $('#odswiez').load(adres_url_ktory_pobiera_same_komentarze); Tam możesz dodać warunek który sprawdza czy jest to zapytanie ajaxowe, żeby działał tylko dla zapytań ajaxowych i problem z głowy.
oryt thank u !
i remove the php !
<div id="twitter-container">
<form method="post" action="submit.php" id="messageForm">
<span class="counter">140</span>
<label for="inputField">What are you doing?</label>
<textarea cols="40" rows="2" tabindex="1" id="inputField" name="inputField"></textarea>
<input type="submit" disabled="disabled" value="update" name="submit" class="submitButton inact">
<span class="latest"><strong>Latest: </strong><span id="lastTweet"></span></span>
<div class="clear"></div>
</form>
<h3 class="timeline">Timeline</h3>
<div id="status" class="statuses"></div>
</div>
oryt and sorry for disturbing you !
<div id="twitter-container">
<form method="post" action="submit.php" id="tweetForm">
<span class="counter">140</span>
<label for="inputField">What are you doing?</label>
<textarea cols="40" rows="2" tabindex="1" id="inputField" name="inputField"></textarea>
<input type="submit" disabled="disabled" value="update" name="submit" class="submitButton inact">
<span class="latest"><strong>Latest: </strong><span id="lastTweet"><!–?=$lastTweet?–></span></span>
<div class="clear"></div>
</form>
<h3 class="timeline">Timeline</h3>
<div id="status" class="statuses"><!–?=$timeline?–></div>
</div>
Why You use: $('div.statuses div:first-child').before(msg) ?
If you use before, you are inserting "before" content. Not into content. http://api.jquery.com/before/ If you wanna replace whats inside $('div.statuses div:first-child') you need to use .html() or .text().
I don't know whow msg looks, so I don't know if you always get everything or only new elements. If everything, then use html() or text(). not .before()
You need to show me html part also ;o)
:D the thing is i want to refresh $('div.statuses div:first-child')…after 5second how can i do that ?
function message()
{
var submitData = $('#messageForm').serialize();
$('.counter').html('<img src="img/ajax_load.gif" width="16" height="16" alt="loading" />');
$.ajax({
type: "POST",
url: "submit.php",
data: submitData,
dataType: "html",
success: function(msg){
if(parseInt(msg)!=0){
$('div.statuses div:first-child').before(msg)
$("div.statuses:empty").append(msg);
$('#lastTweet').html($('#inputField').val());
$('#inputField').val('');
recount();
}
}
});
}
Hi, jokab, what duplication are you talking about? The full example (with <html> tags and all) shows time when page is loaded and updated div. It's just to show, that the changes are made. You can use nested div (the part of code below the full one).
If you have problem with your code, show it, and I will see what is wrong. Basically you just nest div which you update with the one you get value from ;o)
If you can't understand a word from what I'm saying it's because of me (sleepy day ;o))
kokers very nice tutorial but how to avoid the duplication of div thank you…
wao, it help me to sort of a time consuming bug on one of my client website.
Atrhick, what do you mean by “the div jump out of position”?
You mean that id for div is duplicated?
I love he script but it makes the div jump out of position