Advertisement
ganryu

Untitled

Aug 8th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.65 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="UTF-8" />
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6.     <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7.     <title>Document</title>
  8.  
  9.     <script
  10.      src="https://code.jquery.com/jquery-3.4.1.min.js"
  11.      integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  12.      crossorigin="anonymous"
  13.    ></script>
  14.   </head>
  15.   <body>
  16.       <h1>Pokemon Api Tests</h1>
  17.       <section>
  18.         <h1>Pokemons</h1>
  19.          <ol id="lista-pokemon">
  20.  
  21.          </ol>
  22.       </section>
  23.   </body>
  24. </html>
  25. <script>
  26.     const apiUrl = 'https://pokeapi.co/api/v2/';
  27.    
  28.     /**
  29.      * @param {number} id
  30.      * @returns {JQueryXHR}
  31.      */
  32.     function getPokemonById(id) {
  33.         var url = apiUrl + 'pokemon/' + id.toString();
  34.         return $.get(url);
  35.     }
  36.  
  37.     /**
  38.      * @param {string} name
  39.      * @returns {JQueryXHR}
  40.      */
  41.     function getPokemonByName(name) {
  42.         var url = apiUrl + 'pokemon/' + name;
  43.         return $.get(url);
  44.     }
  45.  
  46.     /**
  47.      * @param {Object} data
  48.      */
  49.     function updateList(data, status) {
  50.         var lista = $('#lista-pokemon');
  51.  
  52.         lista.append(
  53.             $('<li>', { text: data.name })
  54.         );
  55.  
  56.         console.log(data);
  57.     }
  58.  
  59.     function fail(error, status) {
  60.         console.error('Status: ' + status);
  61.         console.error(error);
  62.     }
  63.  
  64.     $(function () {
  65.         wantToQuery = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  66.         wantToQuery.forEach(elem => {
  67.             var call = getPokemonById(elem);
  68.             call.then(updateList, fail);
  69.         });
  70.     })
  71. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement