EntropyStarRover

02. Bus schedule

Feb 25th, 2021 (edited)
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let info=document.getElementById("info");
  3.     let nextStopId="depot";
  4.     let departBtn=document.getElementById("depart");
  5.     let arrivebtn=document.getElementById("arrive");
  6.     let curentStopName="Depot";
  7.  
  8.  
  9.     function fetchNext(nextStopId){
  10.         var requestOptions = {
  11.             method: 'GET',
  12.             redirect: 'follow'
  13.           };
  14.          
  15.           fetch(`http://localhost:3030/jsonstore/bus/schedule/${nextStopId}`, requestOptions)
  16.             .then(response => response.text())
  17.             .then(r =>JSON.parse(r))
  18.             .then(r=>update(r))
  19.             .catch(error => kill());
  20.    
  21.     }
  22.  
  23.     function depart() {
  24.         fetchNext(nextStopId);
  25.         departBtn.disabled=true;
  26.         arrivebtn.disabled=false;
  27.     }
  28.  
  29.     function arrive() {    
  30.         info.textContent=`Arriving at ${curentStopName}`
  31.         departBtn.disabled=false;
  32.         arrivebtn.disabled=true;
  33.     }
  34.  
  35.     function update(obj){
  36.       info.textContent=`Next stop ${obj.name}`;
  37.         curentStopName=obj.name;  
  38.         nextStopId=obj.next;    
  39.     }
  40.  
  41.     function kill(){
  42.         departBtn.disabled=true;
  43.         arrivebtn.disabled=true;
  44.         info.textContent="Error"
  45.  
  46.     }
  47.  
  48.     return {
  49.         depart,
  50.         arrive
  51.     };
  52. }
  53.  
  54. let result = solve();
Add Comment
Please, Sign In to add comment