我需要使用回调吗?
Do I need to use callbacks?
本文关键字:回调 更新时间:2023-09-26
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;
//}
};
即使在阅读了一些有关回调的内容后,我也很难理解回调。nodeJs 回调简单示例 这有点帮助,但我仍然不清楚它是如何工作的。在过去的一个小时里,我一直在试图弄清楚如何使用回调来编写它。
此模块由以下方式调用:
ytRetrieve.getVideoLength(youtube_parser(text))
youtube_parser的功能:
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];
}
}
你需要使用回调。 代码youtube_parser(
的问题在于您正在调用函数。 回调是作为参数传递的函数,稍后调用。 如果调用该函数,则返回一个字符串。 getVideoLength
期望函数作为参数,而不是字符串。
请改用getVideoLength(youtube_parser)
。 这实际上传递了youtube_parser
函数本身,以便稍后调用(即当getVideoLength
完成时)。 不过,可能需要(error, url)
youtube_parser
的论据。
这是我想出的解决方案。我能做些什么来增强此代码吗?
谢谢你的帮助。
主.js
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)
});
}
}
优酷.js
var yModule = require('youtube-node'),
nodeYoutube = new yModule(),
apiKey = require('./config');
var youtube = function () {
var self = this;
self.time = null;
self.setAPIKey = function (key) {
nodeYoutube.setKey(key);
};
apiKey.getAPIKey(self.setAPIKey);
self.getVideoLength = function (vData, callback) {
nodeYoutube.getById(vData, function (result) {
callback(self.convertTime(result['items'][0]['contentDetails']['duration']));
})
};
self.convertTime = function (time) {
var reptms = /(?:('d+)DT)?(?:('d+)H)?(?:('d+)M)?(?:('d+)S)?$/;
var days = 0, hours = 0, minutes = 0, seconds = 0, formattedTime;
//if (reptms.test(time)) {
var matches = reptms.exec(time);
console.log(matches);
if (matches[1]) days = Number(matches[1]);
if (matches[2]) hours = Number(matches[2]);
hours += days * 24;
if (hours.toString().length === 1) {
hours = "0" + hours
}
if (matches[3]) minutes = String(matches[3]);
if (minutes.toString().length === 1) {
minutes = "0" + minutes
}
if (matches[4]) seconds = String(matches[4]);
if (seconds.toString().length === 1) {
seconds = "0" + seconds
}
formattedTime = "[" + hours + ":" + minutes + ":" + seconds - 1 + "]";
return (formattedTime);
//}
};
};
module.exports = youtube;
相关文章:
- AngularJS:我可以跳过函数参数回调吗
- 要求未定义JS回调参数
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 自引用回调
- 测试Angular Service解决错误回调中的promise
- 如何将一个JavaScript函数回调为多个函数
- JavaScript回调函数
- 用于回调的javascript参数
- 将json回调数据转换为日期
- 承诺在非节点式回调上使用Bluebird
- 如何在回调函数中执行流
- 回调函数中传递参数的困难(Google Map API Markers)
- Soundcloud Javascript SDK 3.0-回调无法读取属性'connectCallback
- 如何使用jqueryAJAX从页面中回调多个变量
- jquery Onclick函数带有导致双击的回调排序函数
- Javascript点击事件回调不起作用
- 一旦加载并渲染了角度引导typeahead,就使用回调
- 成功回调永远不会被JSONP请求调用
- 如何让程序员在javascript中实现正确的回调
- Angularjs使用“;这个“;promise内的关键字回调