0

How can I access variables in an asynchronous function declared previously? I suppose I have to pass them as parameters but how do I do this?

The problem is, by the time http is loaded, i and other data has changed since the function call is asynchronous.

var myDataInitializedBefore = ...;

for (var i = 0; i < myDataInitializedBefore.length; ++i)
{
    var http = new XMLHttpRequest();
    http.open("GET", myUrl, true);
    http.responseType = "blob";
    http.onload = function(e) 
    {
        if (this.status == 200)
        {
            --> do something with myDataInitializedBefore[i] and some other variables
        }
    }
}
Sceptical Jule
  • 889
  • 1
  • 8
  • 26

0 Answers0