Javascript 我需要使用回调吗?
即使在读了几篇关于回调的文章之后,我也很难理解回调。 这有点帮助,但我仍然不清楚它是如何工作的。在过去的一个小时里,我一直在试图弄清楚如何使用回调编写此文件 此模块正由以下程序调用:Javascript 我需要使用回调吗?,javascript,node.js,callback,Javascript,Node.js,Callback,即使在读了几篇关于回调的文章之后,我也很难理解回调。 这有点帮助,但我仍然不清楚它是如何工作的。在过去的一个小时里,我一直在试图弄清楚如何使用回调编写此文件 此模块正由以下程序调用: var yModule = require('youtube-node'), nodeYoutube = new yModule(); nodeYoutube.setKey("key"); module.exports.getVideoLength = function (vData){ you
var yModule = require('youtube-node'),
nodeYoutube = new yModule();
nodeYoutube.setKey("key");
module.exports.getVideoLength = function (vData){
youTube.getById(vData, function (result) {
return convertTime(result['items'][0]['contentDetails']['duration']);
})
};
var convertTime = function (time){
var reptms = /(?:(\d+)DT)?(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/;
var days = "00", hours = "00", minutes = "00", seconds = "00", formattedTime;
//if (reptms.test(time)) {
var matches = reptms.exec(time);
console.log(matches);
if (matches[1]) days = String(matches[1]);
if (matches[2]) hours = String(matches[2]);
if (matches[3]) minutes = String(matches[3]);
if (matches[4]) seconds = String(matches[4]);
formattedTime = "[" + days + ":" + hours + ":" + minutes + ":" + seconds + "]";
return formattedTime;
//}
};
youtube_解析器的函数:
ytRetrieve.getVideoLength(youtube_parser(text))
您需要使用回调。代码
youtube\u解析器(
的问题在于您正在调用函数。回调函数是作为参数传递的函数,稍后将被调用。如果调用该函数,将返回字符串。getVideoLength
希望函数作为参数,而不是字符串
改为使用
getVideoLength(youtube\u parser)
。这实际上会传入youtube\u parser
函数本身,以便稍后调用(即getVideoLength
完成时)。youtube\u parser
的参数可能需要是(错误,url)
相反。我想出了一个解决方案。我能做些什么来增强这段代码吗
谢谢你的帮助
Main.js
function youtube_parser(url){
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
//console.log(match);
if (match&&match[7]){
return match[7].split(" ")[0];
}
}
var ytempRetrieve = require('./youtube'), ytRetrieve = new ytempRetrieve();
var ytRegex = /(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?/;
bot.addListener('message', function (from, to, text, message) {
if (text.match(ytRegex)) {
console.log(text);
youtube_parser(text, to, ytRetrieve.getVideoLength)
}
});
function youtube_parser(url, to, callback) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
//console.log(match);
if (match && match[7]) {
callback(match[7].split(" ")[0], function (res) {
setTimeout(function () {
bot.say(to, match[7].split(" ")[0] + " is " + res + " long.")
}, 1500)
});
}
}
youtube.js
function youtube_parser(url){
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
//console.log(match);
if (match&&match[7]){
return match[7].split(" ")[0];
}
}
var ytempRetrieve = require('./youtube'), ytRetrieve = new ytempRetrieve();
var ytRegex = /(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?/;
bot.addListener('message', function (from, to, text, message) {
if (text.match(ytRegex)) {
console.log(text);
youtube_parser(text, to, ytRetrieve.getVideoLength)
}
});
function youtube_parser(url, to, callback) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
//console.log(match);
if (match && match[7]) {
callback(match[7].split(" ")[0], function (res) {
setTimeout(function () {
bot.say(to, match[7].split(" ")[0] + " is " + res + " long.")
}, 1500)
});
}
}
“回调函数是作为参数传递的函数,稍后将调用。”我不明白这一点。我该如何处理错误参数?我正在尝试在youtube_解析器返回值后调用getVideoLength。使用我尝试过的解决方案更新,该解决方案为我提供了一个未定义的不是函数error@ECMAScript你还不明白什么?你可以传递一个字符串、一个整数或一个布尔值作为参数。为什么不传递一个fu呢nction?什么是
bot
?是您创建的还是其他模块?@ExplosionPills这是“irc”模块这很有趣。用户名“ECMAScript”询问是否需要回调:)简短回答:是的,总是,尤其是在节点中。这里有一个很好的方法来帮助你把你的头绕在他们身上。干杯