let apiTimeout = setTimeout(fetchAPIData, 1000); function fetchAPIData(){ fetch('API_END_POINT') .then(res => { if(res.statusCode == 200){ // Process the response and update the view. // Recreate a setTimeout API call which will be fired after 1 second. apiTimeout = setTimeout(fetchAPIData, 1000); }else{ clearTimeout(apiTimeout); // Failure case. If required, alert the user. } }) .fail(function(){ clearTimeout(apiTimeout); // Failure case. If required, alert the user. }); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter