script.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Author: Matthew Shiel
  2. $("#user-input-form").submit(
  3. function (event) {
  4. event.preventDefault();
  5. // Store input in a variable
  6. var question = $('#user-input').val().trim() // Remove whitespace
  7. var bot = $('#bot-name').val().trim() // Remove whitespace
  8. // Clear the input box
  9. $('#user-input').val("");
  10. // If the user doesn't input anything return nothing
  11. if (!question) {
  12. return;
  13. }
  14. // Append the user's question to the list and clear input
  15. $("#output-area").append('<li>' + question + '</li>');
  16. // scroll to the bottom of the messages
  17. // adapted from https://stackoverflow.com/questions/11715646/scroll-automatically-to-the-bottom-of-the-page
  18. $('#output-area').animate({
  19. scrollTop: $('#output-area').prop("scrollHeight")
  20. }, 300);
  21. $.get('/user-input', {
  22. value: question,
  23. bot: bot
  24. })
  25. .done(function (data) {
  26. // Set a timeout to make Eliza seem like she's thinking
  27. // Adapted random number in a range from here https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range
  28. setTimeout(() => {
  29. // Add Eliza's answer to the list
  30. addListItem(data);
  31. }, Math.floor(Math.random() * (2000 - 600 + 1)) + 600);
  32. })
  33. });
  34. function addListItem(input) {
  35. // Add the input to the list
  36. $("#output-area").append('<li>' + input + '</li>')
  37. // Automatically scroll to the last message
  38. // adapted from https://stackoverflow.com/questions/11715646/scroll-automatically-to-the-bottom-of-the-page
  39. $('#output-area').animate({
  40. scrollTop: $('#output-area').prop("scrollHeight")
  41. }, 300);
  42. }