why this JavaScript method return undefined?

前端 未结 3 2056
没有蜡笔的小新
没有蜡笔的小新 2021-01-24 08:33

in this code this method return undefined despites alert statement print a value ?

function getNearestPoint(idd) 
        {
            var xmlhttp;
                     


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-24 09:02

    result setting statement executes inside async Ajax function

    Because your second if statement (that sets result value) doesn't get hit. Why not? Because the moment you send an Ajax request you return the result value which is still undefined. Ajax call will execute the anonymous function later on and set this result variable which has been returned long ago.

    Ajax is asynchronous and your code isn't taking this into account.

提交回复
热议问题