Coots commited on
Commit
7d316b3
Β·
verified Β·
1 Parent(s): 419729d

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +15 -7
script.js CHANGED
@@ -3,7 +3,8 @@ document.addEventListener("DOMContentLoaded", () => {
3
  const input = document.getElementById("userInput");
4
  const micBtn = document.getElementById("micBtn");
5
 
6
- // Append a message to the chat box
 
7
  function appendMessage(text, sender = "bot") {
8
  const msg = document.createElement("div");
9
  msg.className = sender === "bot" ? "bot-message" : "user-message";
@@ -12,13 +13,15 @@ document.addEventListener("DOMContentLoaded", () => {
12
  chatBox.scrollTop = chatBox.scrollHeight;
13
  }
14
 
15
- // Send message to backend
16
  async function sendMessage() {
 
 
17
  const message = input.value.trim();
18
  if (!message) return;
19
 
20
  appendMessage(message, "user");
21
  input.value = "";
 
22
 
23
  const loadingMsg = document.createElement("div");
24
  loadingMsg.className = "bot-message";
@@ -27,26 +30,31 @@ document.addEventListener("DOMContentLoaded", () => {
27
  chatBox.scrollTop = chatBox.scrollHeight;
28
 
29
  try {
 
 
 
30
  const res = await fetch("/api", {
31
  method: "POST",
32
  headers: { "Content-Type": "application/json" },
33
- body: JSON.stringify({ question: message })
 
34
  });
35
 
 
36
  const data = await res.json();
37
  loadingMsg.remove();
38
 
39
  appendMessage(data.answer || "❌ No response from AI.", "bot");
40
  } catch (err) {
41
  loadingMsg.remove();
42
- appendMessage("❌ Failed to get response: " + err.message, "bot");
 
 
43
  }
44
  }
45
 
46
- // Make sendMessage available globally for <form onsubmit>
47
  window.sendMessage = sendMessage;
48
 
49
- // Submit on Enter key (in case form fallback fails)
50
  input.addEventListener("keydown", (e) => {
51
  if (e.key === "Enter") {
52
  e.preventDefault();
@@ -54,7 +62,7 @@ document.addEventListener("DOMContentLoaded", () => {
54
  }
55
  });
56
 
57
- // Voice input (mic) support
58
  if ("webkitSpeechRecognition" in window || "SpeechRecognition" in window) {
59
  const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
60
  recognition.lang = "en-IN";
 
3
  const input = document.getElementById("userInput");
4
  const micBtn = document.getElementById("micBtn");
5
 
6
+ let isWaiting = false;
7
+
8
  function appendMessage(text, sender = "bot") {
9
  const msg = document.createElement("div");
10
  msg.className = sender === "bot" ? "bot-message" : "user-message";
 
13
  chatBox.scrollTop = chatBox.scrollHeight;
14
  }
15
 
 
16
  async function sendMessage() {
17
+ if (isWaiting) return;
18
+
19
  const message = input.value.trim();
20
  if (!message) return;
21
 
22
  appendMessage(message, "user");
23
  input.value = "";
24
+ isWaiting = true;
25
 
26
  const loadingMsg = document.createElement("div");
27
  loadingMsg.className = "bot-message";
 
30
  chatBox.scrollTop = chatBox.scrollHeight;
31
 
32
  try {
33
+ const controller = new AbortController();
34
+ const timeoutId = setTimeout(() => controller.abort(), 30000); // optional 30s timeout
35
+
36
  const res = await fetch("/api", {
37
  method: "POST",
38
  headers: { "Content-Type": "application/json" },
39
+ body: JSON.stringify({ question: message }),
40
+ signal: controller.signal
41
  });
42
 
43
+ clearTimeout(timeoutId);
44
  const data = await res.json();
45
  loadingMsg.remove();
46
 
47
  appendMessage(data.answer || "❌ No response from AI.", "bot");
48
  } catch (err) {
49
  loadingMsg.remove();
50
+ appendMessage("❌ Error: " + (err.message || "Unknown error"), "bot");
51
+ } finally {
52
+ isWaiting = false;
53
  }
54
  }
55
 
 
56
  window.sendMessage = sendMessage;
57
 
 
58
  input.addEventListener("keydown", (e) => {
59
  if (e.key === "Enter") {
60
  e.preventDefault();
 
62
  }
63
  });
64
 
65
+ // Voice support
66
  if ("webkitSpeechRecognition" in window || "SpeechRecognition" in window) {
67
  const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
68
  recognition.lang = "en-IN";