简体   繁体   English

回调函数执行多次

[英]Callback Function executing multiple times

I am learning call back functions in Javascript and hence trying to execute a function that uses call back function 我正在学习Javascript中的回调函数,因此尝试执行使用回调函数的函数

 var allUserDdata = []; function logStuf(userData) { if (typeof(userData) === "string") { console.log(userData); } else if (typeof(userData) === "object") { for (var items in userData) { console.log(items, userData[items]); } } } function getInput(options, callback){ allUserDdata.push(options); callback(allUserDdata); } getInput("Alex", logStuf); getInput({ "Name": "Alex", "Place": "Malaysia" }, logStuf); 

Instead of printing the output as: 而不是将输出打印为:

0 Alex
1 {Name: "Alex", Place: "Malaysia"}

Its printing the output as: 其将输出打印为:

0 Alex
0 Alex
1 {Name: "Alex", Place: "Malaysia"}

May I know why? 我可以知道为什么吗? Also, I am in the beginner's phase of learning Javascript so please ignore if its a stupid question. 另外,我处于学习Javascript的初级阶段,因此请忽略它是否是一个愚蠢的问题。

Every time getInput is called, you're push ing to the allUserDdata array, and then the whole allUserData is piped to callback . 每次调用getInput ,您将push allUserDdata数组,然后将整个 allUserDatacallback So if you only want to log the item that getInput was called with and not the whole array, call callback on the item, not the array: 因此,如果您只想记录调用getInput的项目而不是整个数组,请对该项目(而不是数组)进行callback

 var allUserDdata =[]; function logStuf(userData){ if (typeof(userData)==="string"){ console.log(userData); } else if (typeof(userData)==="object") { for (var items in userData) { console.log(items, userData[items]); } } } function getInput(options, callback){ allUserDdata.push(options); callback(options); } getInput("Alex", logStuf); getInput({"Name":"Alex", "Place":"Malaysia"}, logStuf); 

Two things here. 这里有两件事。 Consider this set of inputs 考虑这组输入

getInput("Alex", logStuf);
getInput({ "Name": "Alex", "Place": "Malaysia" }, logStuf);

the output is 输出是

 0 Alex
    0 Alex
    1 {Name: "Alex", Place: "Malaysia"}

this is because the allUserDdata is declared outside all the functions , so the array already contain the previous input. 这是因为allUserDdata是在所有函数外部声明的,因此数组已包含先前的输入。 When the second input is fed to to it will have total two elements, therefore the logStuf will iterate over two elements. 当第二个输入被馈送给它时,它将总共有两个元素,因此logStuf将迭代两个元素。

So you can keep this array inside the getInput function. 因此,您可以将此数组保留在getInput函数中。

Secondly allUserDdata is an array , to iterate an array never use for..in 其次, allUserDdata是一个数组,要迭代一个数组,它永远不会用于..in

 //var allUserDdata = []; function logStuf(userData) { if (typeof(userData) === "string") { console.log('Here', userData); } else if (Array.isArray(userData)) { userData.forEach(function(item, index) { console.log(index, item) }) } } function getInput(options, callback) { var allUserDdata = []; allUserDdata.push(options); callback(allUserDdata); } getInput("Alex", logStuf); getInput({ "Name": "Alex", "Place": "Malaysia" }, logStuf); 

You have called getInput function twice. 您已经两次调用了getInput函数。 Your mistake: you have to set your variable allUserDdata to new array in the function getInput . 您的错误:您必须在函数getInput allUserDdata变量allUserDdata设置为新数组。

 var allUserDdata =[]; function logStuf(userData) { if(typeof(userData)==="string") { console.log(userData); } else if(typeof(userData)==="object") { for(var items in userData) { console.log(items, userData[items]); } } } function getInput(options, callback) { allUserDdata = []; // the mistake: you have to set it to new array allUserDdata.push(options); callback(allUserDdata); } getInput("Alex", logStuf); getInput({"Name":"Alex", "Place":"Malaysia"}, logStuf); 

You have called logStuf function twice 您已经两次调用logStuf函数

First time it logged 0 Alex 第一次登录0 Alex

Second time it logged 0 Alex 1 {Name: "Alex", Place: "Malaysia"} 第二次登录0 Alex 1 {名称:“ Alex”,地点:“马来西亚”}

May be you can call logstuff at the second time only 可能是您只能第二次拨打logstuff

 var allUserDdata = []; function logStuf(userData) { if (typeof(userData) === "string") { console.log(userData); } else if (typeof(userData) === "object") { for (var items in userData) { console.log(items, userData[items]); } } } function getInput(options, callback) { allUserDdata.push(options); if (callback) { callback(allUserDdata); } } getInput("Alex"); getInput({ "Name": "Alex", "Place": "Malaysia" }, logStuf); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM