如何限制javascript函数允许自己被调用的速度? [英] How can I rate limit how fast a javascript function allows itself to be called?

查看:54
本文介绍了如何限制javascript函数允许自己被调用的速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaScript函数,实际上最终会进行服务器端调用。我想限制调用这个函数的速率。

I have a JavaScript function which actually ends up making a server-side call. I want to limit the rate at which this function can be called.

什么是一种简单的方法我可以限制我的javascript函数调用的速度200-500几毫秒左右?我应该使用javascript计时器控件吗?

What is an easy way I can limit how fast my javascript function will get called by say 200-500 milliseconds or so? Should I use a javascript timer control?

推荐答案

首先,您需要确定是否要限制速率,因为您忽略了所有功能在您等待期间进行的呼叫,或者您是否只想简单地排队请求,以确保您每秒不会超过X次请求。

First you need to establish if you want to rate limit in that you disregard all function calls that are made during the period when you are waiting, or whether you want to simply queue up requests so that you ensure you never make more than X requests per second.

如果您想要前一个解决方案(忽略新的函数调用),那么您应该查看 http://documentcloud.github.com/underscore /#throttle

If you want the former solution (disregard new functional calls), then you should look at http://documentcloud.github.com/underscore/#throttle

如果你想限速,这样你就不会每秒调用你的函数超过X次,但不要丢失那些函数调用总之,那么你需要一个完全不同的解决方案。

If you want to rate limit so that you never call your function more than X times per second, but don't lose those function calls altogether, then you need a wholly different solution.

我在写了一个下划线扩展名。 https://gist.github.com/1084831
你可以在 http://jsbin.com/upadif/8/edit#preview

这篇关于如何限制javascript函数允许自己被调用的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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