Events other than 'place_changed' for Google Maps Autocomplete

前端 未结 5 677
庸人自扰
庸人自扰 2020-12-25 09:57

I have an app that currently fires correctly on place_changed.

However, I want to branch search to behave differently when a user has selected an autocomplete entry,

5条回答
  •  佛祖请我去吃肉
    2020-12-25 10:34

    If you add your own input handler (for example catching CR after user entering his own text), autocomplete and your function may call your method back to back. My solution is to use throttle to avoid the repeated call:

    $('#sell_address_input').keyup(function(e){ if(e.keyCode==13){throttle(addressEntered(),1000)}});
    

    ....

    function throttle(callback, limit) {
        var wait = false;             // Initially, we're not waiting
        return function () {          // We return a throttled function
            if (!wait) 
            {                         // If we're not waiting
              callback.call();        // Execute users function
              wait = true;            // Prevent future invocations
              setTimeout(function () 
              {                       // After a period of time
                wait = false;         // And allow future invocations
              }, limit);
            }
        }
    }
    

提交回复
热议问题