I have about 100 pages, every of which has 10-100 images attached. The path to the images are kept in a database.
Then, I have an area at every page where user can see random picture开发者_JAVA百科s from the list of pictures mentioned above. This image changes every 3 seconds.
To archieve such scenario I use a javascript function, which calls itself every 3 seconds.
function GenerateNewImg() {
$.ajax({
url: "myurl.php",
type : "get",
dataType: 'json',
async:true,
success: function(data){
$("#imgtochange").attr("src", data.res);
}
});
t = setTimeout('GenerateNewImg()',3000);
}
And in myurl.php I randomly choose a page and then an image.
I think, this is not very good solution, because it consumes processor time at the server.
Are there better ways to get a behaviour I need?
Represent your file list as JSON, load it in its own script block so it can be cached on the browser. Then read the data from a random position in the dataset. This way you don't have to keep going back to the server.
精彩评论