这可能是一个非常基本的问题,但我从未做过,也做不到到目前为止,所以我要在这里问它!
我在单独的js
文件中有一个函数,它应该从中返回循环数据的消息,但不确定return
必须去哪里!
这是我的代码:
function test(x,y) {
for(let i in y) {
if(y[i].value == 'x') {
return bot.message(y[i]);
}
sleep(1500);
}
}
这没有任何回报。
所以我在这里要做的是从我的循环中发送多条消息,并在每条消息之间留出1.5秒的睡眠时间。
我做错了什么?
你能试试这个吗。。。
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const bot = {
message: (value) => {
console.log('[Bot]', '[Received]', value);
}
};
async function test(x, y) {
for (let i in y) {
if (y[i].value === x) {
bot.message(y[i]);
}
await sleep(1500);
console.log('Woke Up!!!');
}
}
const y = [{ value: 'a' }, { value: 'b' }, { value: 'a' }];
const x = 'a';
test(x, y);
当然,其中也包含了想象力。