2017-04-20 37 views
0

此代碼讀&發送JSON文件的文件
我怎樣才能讓這段代碼爲功能,並呼籲在1號線?讓這對'向loop`一個功能

// sendAllDocument 
    var i,j,tempstring; 
    for (i=0,j=locale.keyboards[msg.text].document.length; i<j; i++) { 
     tempstring = locale.keyboards[msg.text].document[i]; 
     bot.sendDocument(msg.chat.id, tempstring, opts); 
    } 
+0

你知道如何使一個for循環,但不是一個函數...? – Denny

回答

4
function sendAllDocument(msg, opts) { 
    var i,j,tempstring; 
    for (i=0,j=locale.keyboards[msg.text].document.length; i<j; i++) { 
     tempstring = locale.keyboards[msg.text].document[i]; 
     bot.sendDocument(msg.chat.id, tempstring, opts); 
    } 
} 

要撥打:

sendAllDocument(msg, opts); 
+0

找不到名字'opts' –

+0

我已經更新了代碼,允許您將opts傳遞給函數。 – DiderDrogba344

+0

已解決。謝謝。 @ diderdrogba344 –

2
function sendAllDocument(){ 
    var i,j,tempstring; 
    for (i=0,j=locale.keyboards[msg.text].document.length; i<j; i++) { 
     tempstring = locale.keyboards[msg.text].document[i]; 
     bot.sendDocument(msg.chat.id, tempstring, opts); 
    } 
} 

,並調用它sendAllDocument()

+0

如何使用它? –

+0

找不到名字'opts' –

0

創建一個函數:

function sendAll(msg, opts, bot) { 
    locale.keyboards[msg.text].document.forEach(function (document) { 
     bot.sendDocument(msg.chat.id, document, opts); 
    }; 
}; 

並執行它:

sendAll(msg, opts, bot);