jQuery ajax success not changing local variable -


I am trying to use Ajax to check that the image is present before loading it or not. I have done most of this work, but I am setting up a variable inside the error: and success: tag in AJAX, and it is not changing my IMG variable, do any of those tags affect the rest function The way?

  function getImage (imageNum) {var img; $ Ajax ({url: imageLocation + imageNum + '.png', type: 'HEAD', error: function () {img = 0;}, success: function () {var img = $ ('& lt; img / Gt; ', {src: imageLocation + imageNum +' .png ', category:' image ', id:' image '+ imageNum}); return IMG;}}); Console.log (img); Return img; }   

OK, if I have no reason to use AJAX, then I will only use the image tag. The problem is that the variable is not changing anything in my work. Here is an example function getImage (imageNum) {var img; Var success = 10; Var img = $ ('& lt; img / & gt;', {src: imageLocation + imageNum + '.png', category: 'image', id: 'image' + image number, success: work () {success = 1}, error: work () {success = 0;}}); Console.log (success); If (success = 1) returns IMG; And return 0; } Function loadImage (imageNum) {var img = getImage (imageNum); If (IMG! = 0) {$ ('# slider'). Picture load (function () {$ ("# slider"). Append (IMG); load image (image + 1);}); } And return; }

$. Ajax is asynchronous, so you are calling console before logout is completed You can not return like this when your code is synchronous

You must call your other code for example - Example:

  $ Ajax ({url: '', data: '', success: function (data) {codeThatNeedsImg (data);}}); Function codeatatinidesimg (IMG) {console.log (IMG); }    

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -