Hi,
Im working on an application that works on a timer.
There is a timer on a product, and when the timer expires, the application dispatch
the NextProduct method that set the next product in queue to be the active one and resets the timer. The timer is basiclly an Ajax function that's being called every minute and updates the timer on screen. everything seem to work fine, but what happens if no one is viewing the website ?
Does the timer works on the server all the time, or in order for the ajax function to work, some one needs to be on view the website ?

Thanks

ok...well i will tell u the concept behind it...
if u open ur php or ajax code in browser..it remains their until anyone stops it...
So uploading it to server will make it run continuously...no need to worry....
Hope it is clear....

Member Avatar for diafol

If you want the server to keep things going, I'd split the minutes in a day between the number of products being shown. You could set it to have a different product every minute (e.g. 30 products - each product will appear approx. 2 /hour) or 45 products will ensure that each product is shown every 45 minutes. You could split the 24 hours between say 30 products - 48 minutes per product. This may be a bit unfair if your users come from the same part of the world and certain products only show from midnight to 8 o'clock in the morning!

You could do something like this for a change every minute:

<?php
//include DB details and query
while( $data = mysql_fetch_array($result)){
	$array[] = array($data['alt'], $data['img_file']); //place all images and any other data into this array
}
$start = mktime(0,0,0,date('m'),date('d'),date('Y')); // get the unix timestamp for the start of today
$now = time(); //get the unix timestamp for now
$elapsed = floor(($now - $start)/60); //get the elapsed time in minutes since the start of the day
$item = $elapsed%count($array); //get the remainder of the elapsed minutes divided by the number of images
?>
<!-- the php below just shows the 'img_file' value - change the [1] to the position of the 'img_file' within your $array array -->
<img src="<?php echo $array[$item][1];?>" alt="<?php echo $array[$item][0];?>" />

Thought I'd have a go as your question intrigued me.
This can be ajaxified quite easily with a js timeout to run every minute. The js runs this php script and just updates the src value of the image (just return the image filename to the js script and let the js do the updating). If you're using a js library, you could have some fancy transitions too. Just an idea...

<script type="text/javascript">
function timedCount(){
  doTimer();
  t=setTimeout("timedCount()",60000);
}

function doTimer(){
  //do your ajax here
}
</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.