使用coffeescript延迟(debounce)ajax请求 [英] Delay (debounce) ajax request with coffeescript

查看:150
本文介绍了使用coffeescript延迟(debounce)ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的rails应用程序中使用CoffeeScript,我需要延迟一个远程ajax表单提交,我的主要问题是debouncing请求,使表单将发送一次250毫秒已经过去,的形式。

I'm using CoffeeScript in my rails app, and I need to delay a remote ajax form submission, the main problem I'm having is debouncing the request so that the form will send once 250ms has passed without the user typing in the the form.

现在我有一些看起来像这样的东西,显然它不会工作,所以这有一些调试输出,以帮助我。

Right now I have something that looks like this going on, obviously it won't work so this has some debug output in it to help me out.

看看下面的表格,你应该知道我想要去反弹的函数在 element.keyup事件) - > 函数,任何帮助将是惊人的。

Taking a look at the table below, you should know that the function that I want to be "debounced" is within the element.keyup (event) -> function, any help would be amazing!

remoteTable =

  bindEvents: (element) ->
    element.keyup (event) ->
      console.log(event)


  send: (event) ->
    console.log(event)

**几个例子如何工作? **我确实看到underscore.js的方式,但
这里,这是我的旧方法:我只是不能包装我应该怎么这应该wok:

** It would be really helpful if I could gather a few examples of how this works? ** I did see the way that underscore.js does it, but Here, this is my old method: I just can't wrap my head around how this should wok:

http://davidwalsh.name/function-debounce

推荐答案

您可以使用一个简单的超时,不需要花哨。

You can use a simple timeout for this, no need to get fancy.

remoteTable =
  bindEvents: (element) ->
    timeout = null
    element.keyup (event) ->
      clearTimeout(timeout)
      timeout = setTimeout(remoteTable.send, 250)

  send: ->
    console.log('Sending request...')

这篇关于使用coffeescript延迟(debounce)ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆