Please tell me why this cycle runs infinitely? If you put console.log ("123") in front of $ .get, this is displayed infinitely and eventually the browser hangs, if not all windows. Nevertheless, $ .get is not executed at all.

addresa = pictures.split(":") count = addresa.length; nowfile = 0; openw = 4 while(nowfile < count) { $.get("http://192.168.1.108/photos/" + addresa[nowfile], function(data) { console.log(data) str = "data:image/jpg;base64," + data docInfo['content'][openw] = { image: str }; console.log("ban") openw = openw + 1; nowfile = nowfile + 1; ban = ban + 1; pdfMake.createPdf(docInfo).download(addres + '.pdf'); }); }; 

    1 answer 1

    You got a synchronous execution of an infinite loop, which registers asynchronous XHR requests and does not release the stream, thereby not giving them a chance.

    Change the condition of the loop to while (nowfile++ < count) { ... - registers the required number of XHR requests and is completed, after that, they will start processing requests.

    You can read about asynchrony in javascript, for example, here .

    • And here it would not hurt to add that a synchronous request would not solve the problem, since The callback function is passed as an argument to the $.get() method and, accordingly, is not executed directly in the loop. This means that the nowfile increment occurs out of scope. - Deonis
    • 3
      @Deonis, you're not saying something. - Qwertiy
    • @Qwertiy, maybe I don’t explain very well, because I am mostly not a theoretician, but a practitioner. In this case, it is possible that the code will do it for me . )) The request is synchronous and the browser does not torment - the cycle is limited. - Deonis
    • @Deonis, because GET https://fiddle.jshell.net/echo/json/ 404 . A successful callback at 404 is not performed at all. - Qwertiy