根据时间间隔检查变量更改(PHP + HTML)

so i am just wondering if i could do this:

a basic site, nothing on it but 4 different photos that have different priority levels which indicate the time-span of actually displaying an image ( lvl1=1min, lvl2=3min, lvl3=10min...and so on) How could i do this with html and php.. I am not sure if answer is really basic but i cant seem to get my head around it. Is html code running parallel to php code or does an infinite while loop in php stop the whole html code in process?

I was thinking of creating an infinite while loop in but i am worried it would eventualy somehow crash the site?

I guess i dont understand how html code runs..

Thanks to anyone who helps.

For you purpose you can use AJAX which is update your code on given time interval. If you want to set something on a timer, you can use JavaScript's setTimeout or setInterval methods:

setTimeout ( expression, timeout );
setInterval ( expression, interval );

Here expression is a normal function and timeout and interval are integers in milliseconds. setTimeout runs the timer once and runs the expression once whereas setInterval will run the expression every time the interval passes.

So in your case it would work something like this:

setInterval(function() {
    //call $.ajax here
}, 5000); //5 seconds

As far as the Ajax goes, see jQuery's ajax() method. If you run an interval, there is nothing stopping you from calling the same ajax() from other places in your code.

Anymore expression need please let me know.

In the context of the WWW (and simplifying a bit):

  1. PHP runs
  2. The output of PHP is received by the web server
  3. The web server sends the output to the browser
  4. The browser displays the page

You can look at the current time and use that to decide what <img> tag to send to the browser.

You can't change an image already displaying in the browser using server side code. The code has already run. New code won't run unless the browser makes a new HTTP request.

If you want to change the image displaying in the browser you need client side code. For all practical purposes that means JavaScript.

You can use the timer functions to run code after time has passed and DOM to change the HTML elements already on the page.

As OP mentioned. You have to use JavaScript. PHP is for server side processing, once it send final output to web browser you have to request again from the web server. You can use AJAX for your purpose easily.

Understanding bits from your question this may be what you need

  1. PHP code will send all four images to the browser in img tags. You can add priotity to one of the attributes to each tag for example <img data-priority="1" src...
  2. When this code is fully loaded in browser you start a timer in javascript which then looks at current time and subtract it from time it loaded and get number of miliseconds. You can get all this using date and time functions in javascript.
  3. At each time difference you check if that requires an image to be hidden or shown and then show or hide it. Look at jquery show and hide functions for html nodes.

If you want to show one image just hide them all in beginning and then show the one you want according to whatever rule you have either server side or client side. PHP will help you narrow down rules on server and send only required data to client however you can use simple html to hide and show if its not critical and you will avoid making multiple calls to server for images.

If this is the scenario you are looking for then try it in jsfiddle or code with sample images. You can get 4 sample images from http://lorempixel.com/ and use javascript to play with it.