Advertisement
horozov86

Bus Stop

Apr 6th, 2025
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function getInfo() {
  2.     let stopId = document.getElementById('stopId').value;
  3.  
  4.     let info;
  5.  
  6.     let list = document.getElementById("buses");
  7.     list.replaceChildren();
  8.  
  9.     try {
  10.         info = await getBusInfo(stopId);
  11.     } catch (err) {
  12.         onError();
  13.         return;
  14.     }
  15.  
  16.     document.getElementById("stopName").textContent = info.name;
  17.  
  18.     // let list = document.getElementById("buses");
  19.     // list.replaceChildren();
  20.  
  21.     for (let busId in info.buses) {
  22.         let item = document.createElement("li")
  23.         let time = info.buses[busId];
  24.         item.textContent = `Bus ${busId} arrives in ${time} minutes`;
  25.         list.appendChild(item);
  26.     }
  27. }
  28.  
  29. function onError() {
  30.     document.getElementById("stopName").textContent = "Error";
  31.  
  32. }
  33.  
  34. async function getBusInfo(stopId) {
  35.     let url = `http://localhost:3030/jsonstore/bus/businfo/${stopId}`;
  36.  
  37.     let res = await fetch(url);
  38.  
  39.     if (res.status == 204) {
  40.         throw new Error('No information for stop ' + stopId);
  41.     }
  42.  
  43.     let data = await res.json();
  44.     return data;
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement