मैं पहली बार लॉश के throttle
फ़ंक्शन का उपयोग कर रहा हूं और एक एपीआई को कॉल की संख्या को कम करने की कोशिश कर रहा हूं, हालांकि मेरे प्रयासों में मैं इसे कॉल ट्रिगर करने के लिए प्राप्त नहीं कर सकता।
मैंने नीचे एक सरलीकृत संस्करण शामिल किया है:
const _ = require('lodash');
let attempts = 0;
const incrementAttempts = () => {
console.log('incrementing attempts');
attempts++;
console.log("attempts now: " + attempts);
}
const throttledAttempts = () => {
// From my understanding, this will throttle calls to increasedAttempts to one call every 500 ms
_.throttle(incrementAttempts(), 500);
}
// We should make 5 attempts at the call
while(attempts < 5) {
throttledAttempts();
}
इसने मुझे शुरुआत में आउटपुट दिया:
incrementing attempts
attempts now: 1
TypeError: Expected a function
at Function.throttle (/home/runner/SimilarUtterUnits/node_modules/lodash/lodash.js:10970:15)
इस त्रुटि को देखने के बाद मैंने throttle
फ़ंक्शन में एक अनाम आवरण जोड़ने का सुझाव देखा, इसलिए अब मेरा throttledAttempts
ऐसा दिखता है:
const throttledAttempts = () => {
_.throttle(() => incrementAttempts(), 500);
}
हालांकि ऐसा कर रहा हूं... मुझे अब नहीं कंसोल आउटपुट मिलता है!
मैं क्या गलत कर रहा हूं?
0
physicsboy
21 अप्रैल 2021, 15:15
1 उत्तर
सबसे बढ़िया उत्तर
_.throttle
नया थ्रॉटल फ़ंक्शन देता है। आपका कोड कुछ ऐसा होना चाहिए:
let attempts = 0;
const incrementAttempts = () => {
console.log('incrementing attempts');
attempts++;
console.log("attempts now: " + attempts);
}
const throttledAttempts = _.throttle(incrementAttempts, 500);
// We should make 5 attempts at the call
while(attempts < 5) {
throttledAttempts();
}
2
Jesús Domínguez
21 अप्रैल 2021, 15:29
आह! यह बात बताने के लिए धन्यवाद! कुछ गाइड जिन्हें मैं पढ़ रहा था, उन्होंने स्पष्ट रूप से उस ओर इशारा नहीं किया जो मैंने देखा था। बड़ी मदद!
– physicsboy
21 अप्रैल 2021, 15:57
incrementAttempts()
incrementAttempts
को कॉल करता है, औरundefined
को_.throttle
पर भेजता है, क्योंकिincrementAttempts
कुछ भी वापस नहीं करता है।_.throttle(incrementAttempts, 500)
होना चाहिए