THAT Agency Design Studio Blog

Now lets say that you a web page that has a big Block of “something” and inside this “something” you would like to have some information change at a set time interval. Well with Jquery, some PHP and a sprinkle of Ajax, you can get this done quick fast and in a hurry.

The first thing you will need, is obviously the latest jQuery Javascript library file.  and you will also need this light JQuery Timers Plugin.  Now lets set up:

I am going to assume that you know a little about JQuery and say that you already know how to call from inside the “Head” tag. The following is basically the “meat and potatoes” if you will of what is going on.

Let’s Break it down.

1.  First we say “Hey you with the class name of refreshMe, I am going to do something to you every 10 seconds.  (SideNote * 1000 = 1 second; So 5,000 = 5 seconds).

2. Now we use JQuery’s built in Ajax functions to call the URL of the page we want to bring in and have it refresh the element with the class name of refreshMe with the information from this page that called. Follow? Good.

Now since for some deranged reason, I can’t get the code highlighter to work correctly Click here to see the page in action. You will be able to view source from there.

Or download it now.

Share this article

15 Responses to "Refreshing an element at a set time interval using jQuery and a sprinkle of Ajax"

  1. Will this work with an embedded flash object as the element to be changed?
    Is there a maximum time for the time interval?

    Cheers
    Kevin

  2. Hey Kevin.

    To answer your question. Yes this should work for embedded flash objects. If you have these flash objects stored in a Database, then you merely have to create the query in the refresh-me.php file or whatever you decide to name it and just have the ORDER BY RAND() function in your query.

    OR

    you can use a CASE/SWITCH in the refresh-me.php page and set the initial case to something like:

    As far as the maximum time, I don’t see it as an issue, you should be able to place whatever time you feel necessary.

    Hope this helps. If you have any other questions. just drop me another comment.

  3. Hi Rich,
    Thanks for the reply.
    The flash objects are not in a database, all I’m hoping to do is use a flash audio player that calls a xspf play list by number and change the number to a different play list automatically. So I’d probably need to change the whole chunk of code that includes the play lists number.
    Would that work or am I missing something?

    Cheers
    Kevin

  4. [...] Refreshing an element at a set time interval using jQuery and a sprinkle of Ajax Useful function of jQuery that is good and interesting to know about! (tags: jquery) [...]

  5. Kevin, Sorry for such a late reply. Maybe you could have the refresh-me.php page create a random number for the player.

    So in essence, have your player inside the refresh-me page. then when the call is sent at whatever time interval you want you can just generate a random number.

    so if your playlist numbers are from (0-5)

    then do something like:

    $playerRandNum = rand(0,5);

    then use $playerRandNum to assign the playlist number in your flash.

    that could work. although i am just thinking out loud and have no real experience with flash.

  6. [...] Refreshing an element at a set time interval using jQuery & a sprinkling of Ajax [...]

  7. [...] Refreshing an element at intervals using jQuery and Ajax [...]

  8. Hi Rich.

    Modifying the above example to include form elements, like a text box or a radio button, if the end user has selected one of these options/entered information, how do you capture that so when the page refreshes, the values remain intact?

    For example, if the page that gets refreshed as a radio button for 5 items in a list, if the end user selects item 3 via the radio button, is it possible to have the radio button still selected when the page refreshes?

    Thanks,
    Rick

  9. Hey Rick. If i follow you correctly, your saying for example:

    You have 5 items each with a radio button. now if 1 item’s radio button is checked and the page happens to refresh, how do you keep that radio button checked when the page refreshes. is this correct?

    first, the page doesn’t refresh, only the div that you specify refreshes. So if you have the radio buttons inside the same container that would refresh then you would probably have to capture that information when that radio button is checked.

    but if that radio button is not inside the div container that refreshes then there shouldnt be a problem.

    is this for some kind of product piece/page?

    and would the item that got checked, still remain in the container while the other 4 change?

  10. [...] 18. Refreshing an element at a set time interval using jQuery and a sprinkle of Ajax [...]

  11. Hi!

    Do you happen to have a .zip file of your code? Could not open .rar with Ubuntu and I would like to take a look at your code. I’m trying to do so that after file upload tabs get refreshed via Ajax. Thanks!

  12. Oh I didn’t read the article good enough.. I realized now that I’ll see the code from the example site. So thanks again.

  13. [...] Refreshing an element at a set time interval using jQuery and a sprinkle of Ajax | Web Development Now lets say that you a web page that has a big Block of “something” and inside this “something” you would like to have some information change at a set time interval. Well with Jquery, some PHP and a sprinkle of Ajax, you can get this done quick fast and in a hurry. (tags: php ajax jquery) [...]

  14. [...] 18. Refreshing an element at a set time interval using jQuery and a sprinkle of Ajax [...]

  15. Can you use multiple refreshes on a single page with this?
    or would you know if you could with prototypes

    function getFrequests()
    {
    new Ajax.Updater(‘notificationbox’, ‘php/notifications/get_frequests.php’, {
    method: ‘get’, frequency: 1, decay: 1
    });

    }

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Copyright ©2006-2010 THAT Agency, LLC, a web design firm and web develelopment company. All Rights Reserved.
Partner website: THAT SEO Agency