Jump to content

dont answer httprequest immidietly


Catatafish

Recommended Posts

Hello

 

I am wondering if there is some way for the server not to answer httprequests immidietly.

 

I have loaded a php-page that shows the last 20 rows in a mysql table. Then I have a javascript to do a httprequest to another php-page that would check if new rows have been inserted to the table, and if so return them.

 

I know I could have javascript to do this request with certain intervalls, but then the server would send lots of empty answers since most of the time there would not have been new rows inserted. What I want is that the server would wait until there has been a change in the table before answering the request. I tried to do this with a php loop that checked for db-updates, and if there were none, it would sleep for some seconds and then check again, until it found a change, and then echo it and end the loop. This worked but the sleep function does not seem do be ideal since it uses all of the cpu-capacity.

 

I realize that it probably would not be a good idea to have realy long excecution times, but lets say that the server would answer the requests every 30seconds or when ther has been an update in the db. Is this possible? or just a bad idea? Would it be better to just do the httprequests every 5seconds and not to care about all the empty answers?

 

I am using apache 2.2.8 php 5.2 mysql 5.0 on win xp, but if there are solutions with other versions or OS please let me know

Link to comment
Share on other sites

simple. This is how I do it for a forum im working on now. The client wants basically a chat box at the top so you basically want to update the chat field in ajax, so, all you have to do is set a javascript interval. if you only want it to update once use a time out.

 

Interval Example:

<script language=javascript>
window.document.setInterval('function here','5000');
</script>

 

That will execute the function you define every 5 seconds change the 5000 accordingly.

 

Timeout Example:

<script language=javascript>
window.document.setTimeout('function here','5000');
</script>

 

This will execute your function 1 time 5 secs after the code is loaded. Put it right before the </body> tag to allow for it to run 5 secs after the page has loaded.

 

My Example Example:

<script language=javascript>
function sayHi(){
alert('HI');
}
window.document.setInterval('sayHi()','5000');
</script>

 

This annoyingly say hi in an alert box every 5 secs.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.