Loading a different include file every 5 seconds

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm trying to think of a way to load a different include file every 5
seconds on a page - using the AJAX approach of requesting the info every
five seconds, but how can I get the page to refresh just the AJAX code every
5 seconds?

Thanks for any help on this, I'm a bit new to the AJAX stuff.


Re: Loading a different include file every 5 seconds

Quoted text here. Click to load it

I've created a small demo here, just to give you a direction:

This is the PHP code (script source) I used for this, but you can use
any (server-side) programming language you like:

This is how one of the demo files that are included look like:

Please note that the included files need to be without HTML
specification or header information, since the entire text will be
placed inside a <div></div> block.

And, finally, this is what the JavaScript in this demo looks like:

In any case, the point is to use setTimeout() to allow the script to
run again after a certain amount of time (given in milliseconds).

Kim André Akerĝ
- kimandre@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)

Re: Loading a different include file every 5 seconds

to keyboard and typed:

Quoted text here. Click to load it

Javascript. That's what the J in AJAX stands for, after all :-)

If you're new to Ajax, I'd suggest you start by getting hold of the
excellent set of scripts by Mike Chambers, downloadable from here:


If I was trying to solve your problem, this is how I'd do it:

1. Get the above scripts and put MessageLoader.js in my scripts
directory. Then reference that from my web page by including it in the

  <script type="text/javascript"

2. Create a PHP file which will return a different include every time
it runs. It can either do this sequentially, or randomly, whatever you

3. Put something like this into my web page:

  <div id="myIncludeLocation">
  <script language="javascript">
  var rate = new MessageLoader('myIncludeScript.php',  
  function loadData() {
    rate.load('myIncludeScript.php?r=' + Math.random());
  setInterval('loadData()', 5000);

That will load the include file every five seconds and display its

(Disclaimer: I am not an Ajax expert, I've merely used it a few times.
There are probably better ways of achieving what you want than my

Visit: http://www.MotorwayServices.info - read and share comments and opinons
"The sky was made for us tonight"

Site Timeline