回调访问数据
Callback access data
我创建了一个小函数,它检查我提交给它的标签列表,并将好的放在好的数组中,坏的放在坏的数组中。
这一切都是在回调中完成的,所以一旦回调完成,我的其余代码就可以继续了
我对如何从回调访问原始函数中的数据有点困惑。
function findTags() {
validateTags(tags, function () {
//How do I access the bad array here?
console.log('These tags are bad ' + badArray.join(','))
});
}
//This will validate the tags submitted and strip out anything bad from them.
//We will return either success if everything is good or an array of the bad tags to fix.
function validateTags(tags, callback) {
var badArray = new Array(),
goodArray = new Array();
tags = tags.split(',');
for (var i = 0; i < tags.length; i++) {
//If the tag contains just alphanumeric, add it to the final array.
if (tags[i].match(/^[a-z0-9's]+$/i)) {
goodArray.push(tags[i]);
} else {
//Since we didnt match an alphanumeric, add it to the bad array.
badArray.push(tags[i]);
}
}
//console.log(badArray);
//console.log(goodArray);
if (callback) {
callback(badArray);
}
}
简单。只需在回调定义中使用一个参数-
validateTags(tags, function (badArray) {
//How do I access the bad array here?
console.log('These tags are bad ' + badArray.join(','))
});
在验证标记定义中,将badArray
作为参数传递给callback
调用。因此,只需为回调定义定义一个参数,它就会捕获数组作为其值。
相关文章:
- AngularJS-需要在index.html页面中访问来自服务的数据
- 如何使用Wordpress'RESTApi访问本地主机上的数据
- 访问令牌和响应数据
- 如何访问高图表工具提示中的任何特定数据
- 将JSON传递给Jade,并使用相同的数据进行API访问
- 访问Protractor/E2E测试中的$http数据(AngularJS)
- 使用Angular.js访问php数组数据
- 访问Twilio短信元数据
- 访问通过XHR.onreadystatechange回调中的XHR.send(data)发送的数据
- Codeigniter Jquery Ajax,Can't将控制器中的数据访问回html表单
- JavaScript IE 从 json 文件中读取数据 访问被拒绝
- 将twitter api1.0迁移到1.1 json数据访问问题
- 管理数据访问/用户权限
- Javascript json数据访问行和字段
- Javascript是否实现了类似SQL的数据访问
- 在猫鼬之上创建一个额外的数据访问层
- 角度.js数据访问器
- JavaScript数据访问设计
- 在bogart中使用承诺数据访问
- 在初始.ajax调用之外使用JSON数据-访问剩余的JSON数据