简体   繁体   English

函数不会等到所有内容都执行完毕后再继续

[英]Function not waiting till everything is executed before moving on

I am building a chatbot.我正在构建一个聊天机器人。 I have a function as below:我有一个功能如下:

function Smoking(){
   var userinput=document.getElementById("messages").value                   
   window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Do you smoke?");
   if (userinput=="Yes"){                               
   window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Oh no! Smoking is not good for your health!");
}else if(userinput=="No"){                                
   window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Good to hear that you are not smoking!");
 }
}

The Smoking() function is called when user ask the chatbot to ask a question: Smoking() 函数在用户要求聊天机器人提问时被调用:

var convpatterns = [
[".*hi.*", "Hello there!","Greetings!"],
[".*ask me.*", Smoking],
["Yes","You seem quite sure.","OK, but can you elaborate a bit?"]

The window.iSpeech.speak plugin allow the system to speak the words and the plugin is installed correctly and works in other functions. window.iSpeech.speak 插件允许系统朗读单词并且该插件已正确安装并在其他功能中工作。 However when the Smoking() function is called, the system only speak "Do you smoke?", and doesn't wait for user input.然而,当 Smoking() 函数被调用时,系统只说“你抽烟吗?”,而不等待用户输入。 When user typed in the textbox "Yes" or "No", the system moves on and recognise the input text as the "Yes" array instead of the "Yes" within the Smoking() function.当用户在文本框中输入“是”或“否”时,系统继续将输入文本识别为“是”数组,而不是 Smoking() 函数中的“是”。 So if the user typed "Yes", the chatbot will say "You seem quite sure", instead of "Oh no! Smoking is not good for your health!"所以如果用户输入“是”,聊天机器人会说“你看起来很确定”,而不是“哦,不!吸烟对你的健康有害!”

So I was just wondering if there is a way for the function to wait till all lines are executed before moving on?所以我只是想知道是否有办法让函数等到所有行都执行完毕后再继续?

UPDATE更新

I've tried putting them into two separate function but it is still not working:我尝试将它们放入两个单独的函数中,但仍然无法正常工作:

function Smoking(){                 
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Do you smoke?");
SmokingAnswer()
}

function SmokingAnswer(){
var userinput=document.getElementById("messages").value
if (userinput=="Yes"){                             
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Oh no! Smoking is not good for your health!");
}else if(userinput=="No"){                                
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Good to hear that you are not smoking!");
}
}

The SmokingAnswer() function is not executed.不执行 SmokingAnswer() 函数。

The way I understand it is, you want to ask the question to the user by voice, and the user should reply to it in the chat box.我的理解是,你想通过语音向用户提问,用户应该在聊天框中回复。

For your application to work the proper way, you should separate it into multiple JS functions.为了让您的应用程序以正确的方式工作,您应该将其分成多个 JS 函数。 First function gets called when user enters the site, or focuses the chat box.当用户进入站点或聚焦聊天框时调用第一个函数。 This function will ask the question.这个函数会问这个问题。

Then the user enters something, and lets say the function second runs after the user clicks a button.然后用户输入一些东西,假设用户单击按钮后第二次运行该功能。 The second function would then check the input and evaluate the possible answer.然后第二个函数将检查输入并评估可能的答案。 "oh god no ..." / "good to hear..." “哦,天哪……”/“很高兴听到……”

The way your function is designed, is this: once it runs it will ask the question, but it needs the answer already in the chatbox, which at that point is most probably empty since you havent asked the user yet.您的函数的设计方式是这样的:一旦运行它就会提出问题,但它需要聊天框中已有的答案,因为您还没有询问用户,此时聊天框很可能是空的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM