Alright, I'm working on my own signature rotator. At the moment the image only updates when the page is refreshed, which I'm fine with for now.

However, is it possible to force the script to grab a new image after X minutes if the page hasn't already been reloaded? I'd assume no, but figured it was worth asking.

Example (grab new image after 5 minutes)
Person A reloads the page every 3 minutes. Since 3 is less then 5, this wouldn't be called.

Person B reloads the page every 9 minutes. Script waits 5 minutes, updates image to new image, then it continues normally when the page is reloaded.

Recommended Answers

All 4 Replies

You can do it with a combination of ajax and sessions in php. On the client side you'll have to write some js to so that it grabs the image (through ajax) every X minutes. This would be calling something like getsignature.php.

On the getsignature.php file, you'd check the session to see if the user has requested a new image within X minutes. If he hasn't return a new image and update the timestamp in the session to now. If the request is smaller than X minutes, then simply return the existing image.

DemonGal,

There's a couple of ways to go.

1) AJAX: A script, which is scheduled to grab a new image url from a corresponding server-side script every 5 minutes, then DHTML it into the DOM. This is about as simple as AJAX gets.

2) But even simpler : Serve the page with a javascript array of all image urls in the rotation, together with a script scheduled to grab the next (or random) image from the array every 5 minutes. This is self-contained and involves less programming than the AJAX solution.

Personally I would use solution 2 unless there is some reason why the image urls are not known at the time the page is served.

Airshow

@Airshow

She needs state information. Unless you're using a cookie or session you won't know when the last image was served. Since there is a requirement that the image only be reloaded every X minutes, you'd need to keep the state information. In the session or in the cookie.

This point is moot if I've misunderstood the requirement of course :/

This point is moot if I've misunderstood the requirement of course :/

Either you have or I have jomanlk. :ooh:

Given that Javascript is well equipped with setTimeout and setInterval I can't see that a cookie would be necessary.

My solution does assume that on page refresh a new image rotation would start, with timing independent of the previous page's. But even if you needed to coordinate each page with its predecessor, you could pass state fom page to page without using a cookie or setting a session variable. Remember that parametes can be passed in the request (slightly cumbersome but possible).

Airshow

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.