首先注意第二个功能块以获得更好的理解。
所以基本上我有大量的“标签”(变量)在“for(let tag of tagArray){...}”语句中迭代。这些“标签”中的每一个的值都是在“tagFunctionality.addGenericTag(tag.watchTag, function(value) {...}”处从服务器异步请求的。
当我收到大量数据时,这个过程可能会变得很慢;为了提高效率,我建议取消我知道会返回零的请求。在大约 1000 个值之后,回调值开始在大块中变为零,所以我假设我可以忽略这些块并默认为零而不是从服务器解释零值,从而加快完成时间。目前,我被迫遍历每个值并等待服务器返回每个值,然后再继续。我怎样才能成功地停止接收特定的异步调用或类似/更有效的东西?
(请参阅我在“我最近的版本”评论中的部分尝试。)
功能一:
var repetitionLog = [];
var addGenericTag = function(tag, callback) {
// this spot runs before bulk data is received
currentSession.readVariableValue("ns=2;s=" + tag, function(err, dataValue) {
// this spot runs after bulk data is received
if (dataValue && dataValue.value) {
// MY RECENT EDITION: Detect chain of zeros
if (dataValue.value.value == 0) {
repetitionLog.push(dataValue.value.value)
// console.log(repetitionLog.length);
if (repetitionLog.length >= 1000) {
// this spot runs if 1000 zeros have been returned
// skip the next 100 callbacks and reset the repetition log or something
}
}
callback(dataValue.value.value);
} else {
callback(null);
}
});
}
功能二:
var tagArray = []; // pretend this array is full of tags
var generateTagSystem = function(tagArray) {
var tagSystem = function() {
// Connect to this server
tagFunctionality.connect(function() {
// Subscribe to created session
tagFunctionality.start();
// IMPORTANT BIT, iterates over thousands of tags
for (let tag of tagArray) {
// Request value object of a particular tag
tagFunctionality.addGenericTag(tag.watchTag, function(value) {
// Tag is empty
if (value === null) {
// Tag value is empty, error code
// Tag is not empty or zero
} else if (value != 0) {
// Do something with tag code
// Tag is zero
} else {
// possibly useful future spot
}
});
}
});
};
tagSystem();
};