Bobys
Bobys

Reputation: 677

Delay before execution

I've got this JavaScript here:

$('#takePicturebtn').click(function()
  {
    var injectImage = function(id, url) {
      var z = document.getElementById(id);
      z.src=url;  
      }; 
    injectImage("pic", $.getJSON('/picture'));
  });

The $.getJSON('/picture') needs some time to be executed and return the image link. Is it possible to give some time/delay to be executed and then carry on with the process?

Flask function:

@app.route("/picture")
    def picture():
        link = interact().ftpSession('/home/pi/AlarmwebNew/pictures/' + interact().grabPicture(), interact().grabPicture())
        return  jsonify(pictureLink=link)

Upvotes: 0

Views: 105

Answers (3)

DontVoteMeDown
DontVoteMeDown

Reputation: 21465

You have to use a callback. Try this:

$('#takePicturebtn').click(function()
{
    $.getJSON('/picture', function(data) 
    {            
        var z = document.getElementById('pic');
        z.src=data.pictureLink;  
    }); 
});

Upvotes: 1

jAndy
jAndy

Reputation: 236152

A convenient way would be to redesign your injectImage function into accepting a (Promise) object as second parameter instead a string.

var injectImage = function( id, promise ) {
    $.when( promise ).done(function( url ) { 
        var z = document.getElementById(id);
        z.src=url;  
    });    
}; 

Upvotes: 2

Justinas
Justinas

Reputation: 43547

As specified in documentation, there is some callback function:

jQuery.getJSON( url [, data ] [, success ] )

So just do:

$.getJSON('/picture', function(){
    alert('done reading json');
});

Upvotes: 0

Related Questions