简体   繁体   中英

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:

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. However when the Smoking() function is called, the system only speak "Do you smoke?", and doesn't wait for user input. 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. 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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