0

I’m polling S3 every 5 seconds for an image. My polling is successful and I can see it GETs the URL with the image in web inspector. But the function inside of the done() isn’t executing (I can't see anything logging to console):

  (function poll() {
    setTimeout(function () {
      userId = $('#photo').data('user-id');
      photoPath = $('#photo').data('photo-path');
      $.ajax({
        type: 'GET',
        dataType: 'json',
        url: 'http://s3.amazonaws.com/my_bucket/user-photos/'+userId+'/original/'+photoPath,
        done: function (data) {
          console.log(data);
          $("#photo").append(data);
        },
        complete: poll
      });
    }, 5000);
  })();

What am I doing wrong?

3
  • Do you think $("#photo").append(data) will show the image? Commented May 31, 2014 at 14:57
  • setTimeout makes it once with delay of 5 seconds , you need setInterval to make it every 5 seconds Commented May 31, 2014 at 15:17
  • 1
    @MajedDH if you look, the function is being called in done() - this is the correct way because the image may take a short time to load and eventually setInterval will interfere with its previous iterations. Commented May 31, 2014 at 15:20

1 Answer 1

1

You're asking for dataType: 'json' but you won't get that back because the server is sending an image.

Are you wanting to show the image in $('#photo')?

 (function poll() {
    setTimeout(function () {
      console.log('polling');
      userId = $('#photo').data('user-id');
      photoPath = $('#photo').data('photo-path');
      $('<img>').on('load', function(){
          $('#photo').empty().append(this);
          poll();
      }).prop('src', 'http://s3.amazonaws.com/my_bucket/user-photos/'+userId+'/original/'+photoPath);
    }, 5000);
  })();

Demo (with image path replaced by jsfiddle logo)

Sign up to request clarification or add additional context in comments.

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.