- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
找出我的代码有什么问题,或者 underscore.js throttle 器是否正常工作。
我在一个文件中有一个巨大的邮政编码列表,我正在读取这些代码并将它们粘贴到控制台上。
我正在尝试使用 Underscore.js throttle() function ,但是我的代码在两次运行后停止(即使我有几十个),并且从未打印其余值。
我的代码在一个非常简单的 NodeJS 项目中。我创建了一个 MCVE我面临的情况:
"use strict";
//requiremetns
let fs = require('fs');
let readLine = require('readline');
let _ = require('underscore');
//constants
const INPUT_FILE = 'dataset.txt';
const RADIX_CONVERSATION = 10;
const THROTTLE_DELAY = 500;
let init = function() {
let lineReader = readLine.createInterface({
input: fs.createReadStream(INPUT_FILE),
output: process.stdout,
terminal: false
});
let throttledRequestFn = _.throttle(requestFn, THROTTLE_DELAY);
lineReader.on('line', function(line) {
line = line.trim();
if (_.isNaN(parseInt(line, RADIX_CONVERSATION))) {
//Do some stuff
}
else {
throttledRequestFn('mahCountry', line);
}
});
};
let requestFn = function(country, postalCode){
console.log('request for ' + country + ' and postal ' + postalCode + ' done');
return false;
};
init();
在这里,我首先从阅读文件开始,一次一行。然后如果我正在阅读的行是一个数字,我打印一些东西,否则什么都不打印。
以下是测试文件:
Vietnam
000000
100000
160000
170000
180000
200000
220000
230000
240000
250000
260000
270000
280000
290000
300000
310000
320000
330000
350000
360000
380000
390000
400000
410000
420000
430000
440000
460000
480000
510000
520000
530000
550000
560000
570000
580000
590000
600000
620000
630000
640000
650000
660000
670000
700000
790000
800000
810000
820000
830000
840000
850000
860000
870000
880000
890000
900000
910000
920000
930000
940000
950000
960000
970000
在我看来,我的代码应该每秒发出 2 个请求,每个请求之间有 500 毫秒的延迟。它应该打印测试文件中的所有代码。但是,我从未见过超过第二个值的任何东西!为什么会这样?
最佳答案
throttle
函数按预期工作。来自documentation :
Useful for rate-limiting events that occur faster than you can keep up with.
这意味着您的包装函数被调用的次数可能少于您想要的。
您真正想要的可能是某种队列。 Underscore 不提供,但异步库提供:http://caolan.github.io/async/docs.html#.queue
let fs = require('fs');
let readLine = require('readline');
let _ = require('async');
// import the async library
let async = require('async');
const INPUT_FILE = 'dataset.txt';
const RADIX_CONVERSATION = 10;
// create the queue
var q = async.queue(function(task, callback) {
// make sure the queue task calls the callback only after the THROTTLE_DELAY
setTimeout(function () {
requestFn(task.country, task.postalCode);
callback();
}, THROTTLE_DELAY);
}, 1)
q.drain = function () {
console.log('all items have been processed');
};
let init = function () {
let lineReader = readLine.createInterface({
input: fs.createReadStream(INPUT_FILE),
output: process.stdout,
terminal: false
});
lineReader.on('line', function(line) {
line = line.trim();
if (_.isNaN(parseInt(line, RADIX_CONVERSATION))) {
// Do some stuff
}
else {
// Add the line to the Queue, to be executed later
q.push({country: 'mahCountry', postalCode: line});
}
});
};
let requestFn = function(country, postalCode){
console.log('request for ' + country + ' and postal ' + postalCode + ' done');
return false;
};
init();
请注意在处理队列中元素的函数中使用了 setTimeout
。这样一来,您仍然只会每 500 毫秒发出一个请求,但可以保证发出所有请求。
关于javascript - Underscore.js throttle 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38786486/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!