我正在开发一个Node.js应用程序,并使用涡旋命令。我试图将一个值从命令发送到一个函数,但是我无法让它工作。我做错什么了吗?
以下是代码:
vorpal
.command('rollto <num>', 'Rolls to')
.action(function(num) {
rollto(num);
});
function rollto(num) {
bettime = bettimems % 60;
socket.emit('betting', bettime);
timer1 = setInterval(function () {
bettime--;
socket.emit('betting', bettime);
if (bettime == 0) {
socket.emit('random number', num);
console.log("Rolled to:" + num + "!!!");
clearInterval(timer1);
}
}, 1000);
}发布于 2016-01-13 22:25:59
问题是,传递给命令的action的函数具有与假设不同的参数。
这是文档的相关部分
.command.action(function)
This is the action execution function of a given command.
It passes in an arguments object and callback.
Actions are executed async and must either call the passed
callback upon completion or return a Promise.下面是一个有用的例子:
var vorpal = require('vorpal')();
vorpal
.command('rollto <num>', 'Rolls to')
.action(function(arguments, callback) {
rollto(arguments, callback);
});
function rollto(arguments, callback) {
var num = arguments.num; // get 'num' parameter from arguments
timer1 = setInterval(function () {
console.log('test');
console.log(num);
clearInterval(timer1);
callback(); // Don't forget to use callback() to notify vorpal
}, 1000);
}
vorpal
.delimiter('myapp$')
.show();注意,实际上在setInterval中有一个异步代码,所以您需要在末尾使用回调()通知vorpal处理已经完成。
https://stackoverflow.com/questions/34777859
复制相似问题