Throttle event calls in jQuery

前端 未结 7 880
自闭症患者
自闭症患者 2020-11-27 03:52

I have a keyup event bound to a function that takes about a quarter of a second to complete.

$(\"#search\").keyup(function() {
  //code that tak         


        
7条回答
  •  鱼传尺愫
    2020-11-27 03:59

    Something like this seems simplest (no external libraries) for a quick solution (note coffeescript):

    running = false
    $(document).on 'keyup', '.some-class', (e) ->
      return if running
      running = true
      $.ajax
        type: 'POST',
        url: $(this).data('url'),
        data: $(this).parents('form').serialize(),
        dataType: 'script',
        success: (data) ->
          running = false
    

提交回复
热议问题