From b86e67a0d603165a7aed2f3305bf7845435b6536 Mon Sep 17 00:00:00 2001 From: zmagod Date: Fri, 19 Aug 2016 16:38:34 +0200 Subject: [PATCH] refactor initProjectExperiment --- app/assets/javascripts/projects/show.js | 47 ++++++++++++++----------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/app/assets/javascripts/projects/show.js b/app/assets/javascripts/projects/show.js index b61455baa..8be597218 100644 --- a/app/assets/javascripts/projects/show.js +++ b/app/assets/javascripts/projects/show.js @@ -1,30 +1,38 @@ (function(){ + var count = 0; function initProjectExperiment(){ var url = $("[data-lupdated-url]").data("lupdated-url"); var updated = "[data-id=" + $("[data-lupdated-id]").data('lupdated-id') +"]"; var el = $(updated).find("img"); - el.hide(); var timestamp = el.data("timestamp"); var img_url = $(updated).find(".workflowimg-container").data('updated-img'); - animateSpinner($(updated).find(".workflowimg-container"), true); - $.ajax({ - url: url, - type: "GET", - data: { "timestamp": timestamp }, - dataType: "json", - success: function (data) { - getNewWorkforwImg(el, img_url, updated); - animateSpinner($(updated).find(".workflowimg-container"), false); - }, - error: function (ev) { - if (ev.status == 404) { - setTimeout(initProjectExperiment(), 200); - } - animateSpinner($(updated).find(".workflowimg-container"), false); - } - }); + animateSpinner($(updated).find(".workflowimg-container"), true); + checkUpdatedImg(el, img_url, url, timestamp, updated); + animateSpinner($(updated).find(".workflowimg-container"), false); + } + + + function checkUpdatedImg(el, img_url, url, timestamp, updated){ + if (count !== 100){ + $.ajax({ + url: url, + type: "GET", + data: { "timestamp": timestamp }, + dataType: "json", + success: function (data) { + getNewWorkforwImg(el, img_url, updated); + animateSpinner($(updated).find(".workflowimg-container"), false); + }, + error: function (ev) { + if (ev.status == 404) { + setTimeout(checkUpdatedImg(el, img_url, url, timestamp, updated), 200); + } + count++; + } + }); + } } function getNewWorkforwImg(el, url, updated){ @@ -35,10 +43,9 @@ success: function (data) { el.html(data.workflowimg); animateSpinner($(updated).find(".workflowimg-container"), false); - }, error: function (ev) { - animateSpinner($(updated).find(".workflowimg-container"), false); + // TODO } }); }