JQuery 等待 x 秒。调用函数之前的不活动状态
我有一个包含多个按钮、滑块等的表单,当单击、拖动等时,它们都会触发主要的 ajax 定价功能。通常会在短时间内连续多次单击一个按钮(例如,用于增加值的 + 按钮) )。
我希望 JQuery 在最后一个触发事件完成后等待 1 秒再执行,而不是每次都调用主 ajax 函数。换句话说,如果函数被触发<自上次触发以来已过去 1 秒...等待。
I have a form with multiple buttons, sliders, etc. that all trigger a main ajax pricing function when clicked, dragged, etc. Often a button is clicked multiple times consecutively in a short amount of time (e.g. a + button to increase a value).
Instead of calling the main ajax function each time, I would like JQuery to wait for 1 sec after the LAST trigger event is done before executing. In other words, if the function is triggered < 1s since the last trigger... wait.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次发生变化时,我都会调用 setTimeout ,并将超时设置为 1000 毫秒。存储 setTimeout 调用返回的值,如果在 setTimeout 调用触发其回调之前发生更改,则取消 setTimeout 并启动另一个 setTimeout。
I would do a setTimeout call each time something changes, and set the timeout to 1000 ms. Store the returned value from the setTimeout call, and if a change occurs before the setTimeout call fires its callback, cancel the setTimeout and start another one.
您可以使用布尔变量作为标志并使用
setTimeout
来实现此目的。它可能看起来像这样。You can achieve this using a boolean variable as a flag, and using
setTimeout
. It might look like something like this.