0

I want to do a Database request each 5 secounds. And if there is a new DB insert, I want to reload a Div!

My brain is burning I can't doing this without a tipp. Please help me

3 Answers 3

1

How about this...

Have a table that contains the "last update" date and nothing else. Then you can perform a very small query to see if that date has changed and then reload the page.

So when you insert (or update if you are worried about those too) you update the date in the last update table.

Your jQuery call should be a get request to a page that just outputs the date.

You compare the date with the one you obtained originally and if it has changed, reload.

$.get("http://yoursite/lastupdate.php", function (data) {
    if (data != originalDate) {
        document.location.reload();
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

you don't need an entire table for 'last update'. each row should have it's own timestamp anyway. might as well return the new rows rather than checking if there even are new rows first..
@Mark - getting the single row from "lastupdate" may be more performant than getting the MAX timestamp from a large table - especially given that this is going to be called every 5 seconds.
but if he wants to update the div via ajax, he's going to have to query again for those new rows anyway. but i guess if most of the time there isn't anything new...then fine.
0

store the date/time the page was loaded into a javascript variable. use setTimeout to run an ajax request every 5 seconds, passing the datetime var. return any rows added after this time. if any rows are returned, update the variable and the div.

Comments

0

PeriodicalUpdates

You could try to use Jquery's PeriodicalUpdates plugin from Github.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.