#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    3
    Rep Power
    0

    Question Javascript:Update Image ONLY when Image changes!


    This is a Javascript question...

    I have a page that shows data on a png file. I have written Javascript to update the image at a given frequency.

    CODE SNIPPET:
    if (document.images) {
    setTimeout('reloadImage()',10000);
    }

    function reloadImage() {
    var now = new Date();

    var num_imgs = 10;
    for (var i=1; i<=num_imgs; i++) {
    if (document.images[i]) {
    URL = document.images[i].src.split("?");
    document.images[i].src = URL[0] + '?' + now.getTime();
    }
    }
    setTimeout('reloadImage()',10000);
    }

    Which works great, except I get a HUGE amount of data transfer in a 24hour period. The 10 sec frequency is fast, but assume it is needed for this application.

    What I would like to do, is check the image to see if it has been changed and only then update it.

    I have some working knowledge of client side javascript but very little on server side and not sure the direction to go. I tried img.fileCreatedDate, but not enough resolution. Any other good ideas would be appreciated.

    I would be willing to re-work and not use Javascript if there is an good answer.

    Thanks,
    Mark
  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    I get the impression that you're generating the image with a script? If you can get the script to return a Last Modified header:

    Last-Modified: Thu, 31 Oct 2002 23:36:27 GMT

    that indicates when the image last changed, the browser won't download the image unless it's been updated. If you do this, though, you can't pass the current time through the url, since the browser will assume it's a different resource you're trying to access.
    -james
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    3
    Rep Power
    0
    The images are actually generated by a perl program using GD::graph library. The system querys a database and generates a plot of the data. The web users may watch the updates on the page as the changes take place.

    The perl script that generates the image, does also create a web page but I don't necessarily tie the image to a single page.

    I found a tool called JSRS (Javascript Remote Scripting) which, I think, will allow a timed javascript event call and communicate with a perl script. My hope is the perl program will tell the javascript if an image has changed.

    Does Javascript support socket communciations??

    Mark
  6. #4
  7. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Javascript is a scripting language, which means it supports whatever you it is you're scripting (a script is just a set of instructions for something else to execute). In this case, you're scripting a browser, so probably no sockets. Also, keep in mind that most javascript is run in an unprivileged environment, so you generally have access to very little of the underlying system.

    Really thought, most browsers have built-in caching mechanisms, so all you have to do is only tell the browser it's a new image when it's actually a new image. The browser won't download stuff that's already in its cache and hasn't changed since it last grabbed it.
    -james

IMN logo majestic logo threadwatch logo seochat tools logo