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)
asked Nov 10, 2014 at 13:40
Bobys
6771 gold badge15 silver badges38 bronze badges
3 Answers 3
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;
});
};
answered Nov 10, 2014 at 13:45
jAndy
237k57 gold badges313 silver badges363 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Bobys
Thanks for your answer so my promise will be $.getJSON('/picture')? $.when($.getJSON('/picture')).done Did I get it right?
jAndy
@user3207652 Actually the code should work "as is", just replace this version with yours.
Bobys
But where shall I include the getJASON function? Thats what I dont understand. Thank you for your help
You have to use a callback. Try this:
$('#takePicturebtn').click(function()
{
$.getJSON('/picture', function(data)
{
var z = document.getElementById('pic');
z.src=data.pictureLink;
});
});
answered Nov 10, 2014 at 13:44
DontVoteMeDown
21.5k10 gold badges72 silver badges113 bronze badges
19 Comments
Bobys
Im a bit lost here with the json , json.id, and json.url. the $.getJSON('/picture') returns a link of a picture like domain.name/image.jpg and I would like to put use that link in an img tag: <img id="pic" src="" class="img-responsive"> could you give me more info about your implementation please? Thanks and really sorry for my noobness
DontVoteMeDown
@user3207652 Sorry, was me that doesn't understood your code. I have updated it. The
src parameter will have a string with the source of the image, right? So just set it to the src attribute of the image element with id pic. May that id change?Bobys
Thanks for your reply.. Well src will have to get the value of getJSON(which is a picture link). The concept of the script is to execute the JSON function and then pass the value(link) to the img tag with id="pic". F.E <img id="pic" src="" //The source here should change dynamic. from the script\\ class="img-responsive"> I dont know if you get exactly what I need to do. Not native english, but Im doing my best.
Bobys
The flask function that returns the link has been added to the original question. I though you mirth find it useful for a correct advices.
Bobys
I've tried to show an animated loading picture by setting the src on the image tag equal to the animated.gif. The first time it show the loading picture, and then the picture was taken shows up. Every thing's great now. But closing the modal and pressing the button again, the modal shows up with the previous picture. So by showing another picture it doesnt help. I'll try your other suggestions as-well. I appreciate your help. Thank you very much!
|
As specified in documentation, there is some callback function:
jQuery.getJSON( url [, data ] [, success ] )
So just do:
$.getJSON('/picture', function(){
alert('done reading json');
});
answered Nov 10, 2014 at 13:44
Justinas
43.9k5 gold badges72 silver badges108 bronze badges
Comments
lang-js
setTimeoutto deal with async programming - as it may accidentally solve the user's problem