How to structure javascript callback so that function scope is maintained properly

后端 未结 4 509
小蘑菇
小蘑菇 2020-12-13 02:56

I\'m using XMLHttpRequest, and I want to access a local variable in the success callback function.

Here is the code:

function getFileContents(filePat         


        
4条回答
  •  再見小時候
    2020-12-13 03:18

    I ran into a similar problem. My code looked like this:

    for (var i=0; i
    
    

    The solution is to replace "xmlHttp" with "this" so the inner function is referring to the correct instance of the request object every time the callback is called.

提交回复
热议问题