- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一项任务是编写自己的 throttle 函数。它需要使用 setTimeout 通过一定数量的测试。
这是我的代码:
var throttle = function(func, delay) {
var counter = 0;
var calledOnce = false;
setInterval(function(){
counter++;
}, 1);
return function() {
if (counter > delay || !calledOnce) {
calledOnce = true;
counter = 0;
return func.apply(this, arguments);
}
};
};
我正在使用以下内容对其进行测试:
var callBack = function () {
console.log('called');
};
var func = throttle(callback, 1000);
func(); // should be called
setTimeout(func, 500); // should not be called
setTimeout(func, 1000); // should be called
setTimeout(func, 1500); // should not be called
setTimeout(func, 1900); // should not be called
但是,当我按照此处的方式运行我的代码时,该函数仅被调用一次,使用原始 func() 调用,并且没有调用 setTimeout 内的任何函数。
我的代码或使用 setTimeout 进行的测试是否有任何明显的问题?
最佳答案
仅仅因为您将它设置为以 1 毫秒的间隔运行并不意味着该浏览器会这样做。例如,如果我将它设置为 0 以强制它采用尽可能小的间隔并多次执行此操作以获得平均值,我发现我可以使用的最小间隔是 ~6 毫秒。在重负载的情况下,这会显着增加。
var start = new Date();
var i = 0, interval = setInterval(function(){
if (++i >= 1000) {
var end = new Date();
var result = (end-start)/1000;
$('#result').text("The average interval was "
+result+" milliseconds");
$('#browser').text(navigator.userAgent);
clearInterval(interval);
}
}, 0);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<p id=result>Please wait about 10 to 20 seconds ...</p>
<p id=browser></p>
不算自己写的,算annotated underscore source throttle function :
Returns a function, that, when invoked, will only be triggered at most once during a given window of time. Normally, the throttled function will run as much as it can, without ever going more than once per wait duration; but if you’d like to disable the execution on the leading edge, pass {leading: false}. To disable execution on the trailing edge, ditto.
_.throttle = function(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};
var later = function() {
previous = options.leading === false ? 0 : _.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function() {
var now = _.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
来自 http://underscorejs.org/#throttle
throttle_.throttle(function, wait, [options])
Creates and returns a new, throttled version of the passed function, that, when invoked repeatedly, will only actually call the original function at most once per every wait milliseconds. Useful for rate-limiting events that occur faster than you can keep up with.
By default, throttle will execute the function as soon as you call it for the first time, and, if you call it again any number of times during the wait period, as soon as that period is over. If you'd like to disable the leading-edge call, pass {leading: false}, and if you'd like to disable the execution on the trailing-edge, pass {trailing: false}.
var throttled = _.throttle(updatePosition, 100);
$(window).scroll(throttled);
关于javascript - 创建自己的 throttle 函数并使用 setTimeout 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33687757/
上下文:我正在 javascript tutorial 的任务下编写一个简单的 throttle 。 任务:编写一个像这样工作的 throttle : function f(a) { consol
我有一个带有 React 函数 component 的简单点击事件,我尝试使用 RxJS throttleTime 进行 throttle 。每次单击我都会在 500 毫秒 内 throttle ,但
在 Laravel 6 中,密码代理现在具有以下功能来限制密码重置( https://github.com/laravel/framework/blob/6.x/src/Illuminate/Auth
我有一个事件监听器,我正在尝试用 lodash.throttle 包装它: import throttle from "lodash.throttle" const throttledHandleRe
当我尝试使用 go get 命令安装 throttled 时: go get "github.com/throttled/throttled" 我得到错误: 无法加载包:包 github.com/th
更具体地说,我正在寻找 BlackBerry 6.0 API Animator 类,其构造函数描述为“创建一个以指定帧速率限制 update() 调用的 Animator 对象”。 http://ww
在应用洞察中,在 API->Activity & Errors 下有与“Api Throttling”和“API Throttling Warnings”相关的部分。但是我找不到关于这些部分的含义以及
最近,我们在使用IPP数据服务的应用中,不时遇到这些错误。 ErrorRequest 2012-12-07T10:10:59+00:00 3001 messag
最近,我们在使用IPP数据服务的应用中,不时遇到这些错误。 ErrorRequest 2012-12-07T10:10:59+00:00 3001 messag
我有以下代码: IDisposable subscription = myObservable.Throttle(TimeSpan.FromMilliseconds(50), RxApp.MainTh
我有一个定义 onDragEvent 的 Controller : controller = Em.Object.create( { onDragEvent: function() {
我如何使用 ScheduledThreadPoolExecutor、ScheduledFuture 和 ExecutorCompletionService 的组合来限制接受可变参数?收到来自 Call
我在 trait ThrottlesLogins 中添加了以下方法在 Laravel 5.5 中 protected function TotalRegisterAttemptsLeft($reque
我正在使用 ThrottleRequest 来限制登录尝试。 在 Kendler.php 我有 'throttle' => \Illuminate\Routing\Middleware\Throttl
我可能对此完全疯了,但是YouTube视频的下载/缓冲速率似乎在一开始就更快,并且缓冲点距当前播放的标记越远,缓冲的速度就越慢。 我疯了吗,还是对其他所有人一样? 假设是后者,那么关于它们如何做的任何
我在我的几个页面上设置了无限滚动功能。它工作得很好,但是加载更多项目的 Ajax 调用会进行多次数据库调用,并且每次都必须加载大量图像,并且通常需要几秒钟才能加载。根据我的连接情况,我将其计时在 3
加特林世界的新手,但一位经验丰富的 Loadrunner 用户。我创建了一个示例模拟来运行两个场景,每个场景有 10 个用户,并且希望运行 10 分钟。以下是我的 setup 函数中的内容。但每次我运
我想知道是否有办法执行诸如 System.out.println(); 之类的操作或记录已发生的限制。目前我可以查看是否发生限制的唯一方法是将rejectExecution 设置为True。问题在于,
我正在使用一个 API,它只允许您使用像 request-promise 或 axios 这样的 promise 请求库每秒进行 200 次调用(1000 毫秒)怎么可能你使用 rx.js 去抖/限制
我正在以用户在多线程环境中定义的批量大小写入内存分布式数据库。但是我想限制写入ex的行数。 1000 行/秒。这个要求的原因是我的生产者写得太快而消费者遇到叶内存错误。在批处理记录时是否有任何标准做法
我是一名优秀的程序员,十分优秀!