Jquery : setInterval or setTimeout
Posted by chaabant on Dec 31, 2008
I was doing a live counter for some of my sites … so to make it “LIVE” I’ve used the Great Jquery library (if you dont know what’s jquery is … i will make a post soon about it and some JQUERY For dummies post)
What i have is simple :
- count.php // the counter basically it’s just a php file where i update the database
- index.php // the main page where i show the counter .
so this is what i wrote on the index.php
1 2 3 4 5 6 7 8 9 10 11 12 |
// The call to the count.php page , Using Post Method and sending 1 as a variable function update() { $.post("count.php", {count:1}, function(data){ $("#counter").html(data);}); } // The code that call the update function each seconds $(document).ready(function() { setInterval("update()", 1000); }); |
// The call to the count.php page , Using Post Method and sending 1 as a variable
function update() {
$.post("count.php", {count:1},
function(data){
$("#counter").html(data);});
}
// The code that call the update function each seconds
$(document).ready(function() {
setInterval("update()", 1000);
});
So after writing this code , everything was working fine and the counter was live and the update worked .
I asked in few forums to see if there was any problem with my code since i’m new to jquery
here are some of the answers i got :
From a Sitepoint member hexburner
You could use an Ajax request with the if Modified header.
Or you could just use the load function.
The one second interval creates a huge amount of requests, which I wouldn’t recommend…
Nobody is really looking to the counter every second, so you can increase the interval without upsetting anybody.
What really concerns me is what happens if counter.php could not be loaded within the one second interval.
Imagine someones internet connection breaks down right after they visit your website.
The page counter.php could not be loaded, and after 30 seconds you’ll have 30 open connections…
So don’t use setInterval with ajax requests, but rather use setTimeout after the ajax request completed or returned an error.
I would recommend something like below, but you could use one of the other ajax functions of jQuery, whichever suits you best.
1 2 3 4 5 6 7 |
jQuery(document).ready(loadCounter); function loadCounter () { $("#counter").load("count.php", {count:1}, function(responseText, textStatus, XMLHttpRequest){ setTimeout(loadCounter, 5000); }); } |
jQuery(document).ready(loadCounter);
function loadCounter () {
$("#counter").load("count.php", {count:1}, function(responseText, textStatus, XMLHttpRequest){
setTimeout(loadCounter, 5000);
});
}
I tested his code and everything was fine and working .
The only concern i’m still having is the server issue , since the site is on a shared account … here is what another person from Google jquery group answered ”
If this is what you are after, and you are getting that data from a database, then a request must be sent every time to that database.
Seems to be pretty taxing, especially if traffic ramps up.
Joe
he’s right , in worse case what i will do is one of the followings :
- Make the call every 5 seconds instead of 1
- Make the call only on click call
Any other ideas for live counter ?
what about the live chat i think it’s the same idea behind …
PHP SOURCE CODE
Posted by chaabant on Dec 24, 2008
User of this site can from now give a direct link to their source code in case they need some help debugging php .
We are happy to announce that TUTORIALS FOR PHP will be offering the following service :
http://codes.tutorials4php.com/
In case you are having a problem in one of you’re website , you can copy – past the php source code where you are having problem and we would be more than happy to help you debugging it … of course if we can !
All what you will have to do is give us the link , and the error you are receiving using our contact form on the top
any questions ?
What does PHP means
Posted by chaabant on Dec 22, 2008
As you know , on this site you can ask PHP Questions , and if u know the answers i will be more than happy to answer them .
To ask a question simply click on the HEADER TAB (PHP QUESTIONS) And submit the form .
Here is the recent question i received :
vikas wrote:
What is The full form of PHP?
1) Personal Home Page
2) PHP Hypertext PreprocessorPlz give me description of the option?
Regards,
Vikas
I think you mean what does PHP Stand for …
it does stand for Hypertext Preprocessor it’s A script language and interpreter .
So the Answer would be (2) , a Personal Home Page would be the main page of a website where you could have personal stuff or maybe a blog where you write your life experiences …
ex: this is my personal blog : CHAABAN , the first page is called the home page .
Hope this answered your question