From 552f616305809d111f7540cbea93f89fad3d0760 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sat, 15 Jun 2019 10:08:06 -0500 Subject: Fix retry on timeout for AJAX requests --- assets/js/embed.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'assets/js/embed.js') diff --git a/assets/js/embed.js b/assets/js/embed.js index 283bc06d..1f742864 100644 --- a/assets/js/embed.js +++ b/assets/js/embed.js @@ -1,5 +1,5 @@ -function get_playlist(plid, timeouts = 1) { - if (timeouts >= 10) { +function get_playlist(plid, retries = 5) { + if (retries <= 0) { console.log('Failed to pull playlist'); return; } @@ -18,7 +18,6 @@ function get_playlist(plid, timeouts = 1) { xhr.responseType = 'json'; xhr.timeout = 20000; xhr.open('GET', plid_url, true); - xhr.send(); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { @@ -51,10 +50,17 @@ function get_playlist(plid, timeouts = 1) { } } + xhr.onerror = function () { + console.log('Pulling playlist failed... ' + retries + '/5'); + setTimeout(function () { get_playlist(plid, retries - 1) }, 1000); + } + xhr.ontimeout = function () { - console.log('Pulling playlist timed out... ' + timeouts + '/10'); - get_playlist(plid, timeouts++); + console.log('Pulling playlist failed... ' + retries + '/5'); + get_playlist(plid, retries - 1); } + + xhr.send(); } if (video_data.plid) { -- cgit v1.2.3