簡體   English   中英

在 for 循環中調用函數 - JavaScirpt

[英]Calling a function within a for loop - JavaScirpt

嗨,我正在嘗試在 for 循環中調用一個函數,但它不起作用......這是我的代碼目前的樣子:

bot.on('message', data => {
    if (data.type !== 'message' || data.subtype === 'bot_message') {
        return;
    }
    findClassroomMention(data,text);
});

var classrooms = 
    {
        L108: ["lokaal 108","L108","108"],
        L208: ["lokaal 208","L208","208"]
    };

function findClassroomMention(message) {    
    var found = false   
    for(var ClassroomId in classrooms) {
        for(var term of classrooms[ClassroomId]) {
            if(message.includes(term)) {
                found = ClassroomId;
                notifyProblemSolver();
                break;
            }
        }
        if (found) notifyProblemSolver(); break;
    }
    return found
};

function notifyProblemSolver(ClassroomId) {
    const params = {
        icon_emoji: ':smiley:'
    }
    bot.postMessageToChannel('caris','We have a problem in' + ClassroomId, params);
};

我希望在 for 循環中調用函數 notifyProblemSolver() ......但是如果我運行代碼,它就不起作用。 有小費嗎? 提前致謝!

我想if (found) notifyProblemSolver; break; if (found) notifyProblemSolver; break; 是問題。 無論if (found) ,都會調用該break ,因此for(var ClassroomId in classrooms) {將只運行一次。

我想你的意思是

if (found) { notifyProblemSolver(); break; }

我在函數末尾看到了一些不必要的分號。 還有部分:

if (found) notifyProblemSolver; break;

應替換為:

if (found) notifyProblemSolver(); break;

因為您在這里調用的是一個函數,而不是一個表達式。

讓我知道這個是否奏效。

完整代碼修改在這里: https : //jsfiddle.net/terza_terza/ms9xLrzu/3/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM