0

I'm trying to work through a question previously asked on stckoverflow - "Using AJAX / jQuery to refresh an image"

Using AJAX / jQuery to refresh an image

The URL from image_feed.php is supposed to change everytime. However, I can't figure out what the code for image_feed.php should be (even an example). Can anyone help?

FYI, my index.php is:

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
    var $img = $('#image1');
    setInterval(function() {
        $.get('image_feed.php?CAMERA_URI=<?=$camera_uri;?>', function(data) {
            var $loader = $(document.createElement('img'));
            $loader.one('load', function() {
                $img.attr('src', $loader.attr('src'));
            });
            $loader.attr('src', data);
            if($loader.complete) {
                $loader.trigger('load');
            }
        });
    }, 5000);
});
</script>
</head>
<body>

<div id="image1">
</div>
</body>
1
  • $loader.one('load', function() should be $loader.on('load', func Commented Jul 15, 2013 at 6:16

3 Answers 3

1

The image_feed.php should just return the image's src as the response.

<?php
// produce the src with your logic.
$src = "https://www.gravatar.com/avatar/daa6ae7c970d99c6c2b3a9d8895aaa1e?s=32&d=identicon&r=PG";
echo $src;
Sign up to request clarification or add additional context in comments.

1 Comment

This was what I was originally looking for. Thanks!
0

Try this:

<!DOCTYPE html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script>
            $(document).ready(function() {
                var $img = $('#image1');
                setInterval(function() {
                    $img.attr('src', 'image_feed.php?CAMERA_URI=<?=$camera_uri;?>');
                }, 5000);
            });
        </script>   
    </head>
    <body>
        <img id="image1" src="image_feed.php?CAMERA_URI=<?=$camera_uri;?>">
    </body>
</html>

1 Comment

This helped as what I had originally wasn't right. Thank you.
0

Try this

$(function() {
  var $img = $('#image1');
  var loading = false;
  setInterval(function() {
    if ( loading === true ) {
      return;
    }
    loading = true;
    var image = new Image;
    image.src = <?php echo json_encode( "image_feed.php?CAMERA_URI=".urlencode( $camera_uri ) ); ?>;
    image.onload  = function() {
      loading = false;
      $img.attr("src",this.src);
    };
    image.onerror = function() {
      loading = false;
      // do something here
    };
  }, 5000);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.