function scrollToBottom (textarea) {
    var maxScrollHeight = textarea[0].scrollHeight;
    textarea.scrollTop(maxScrollHeight)
}

var logRefreshTimeouts = [];
function setNextLogRefreshTimeout (logTextarea, action) {
    var logRefreshTimeoutInMilliseconds = 4000;
    var nextTimeout = setTimeout(function() {
                                    liveUpdateLog(logTextarea, action);
                                 }, logRefreshTimeoutInMilliseconds);
    logRefreshTimeouts.push(nextTimeout);
}

function resetAllLogRefreshTimeouts() {
    for (var i = 0; i < logRefreshTimeouts.length; i++) {
        clearTimeout(logRefreshTimeouts[i]);
    }
    logRefreshTimeouts = [];
}

function updateLog(logTextarea, action, onSuccess) {
    var onSuccess = onSuccess || function() {};
    var benchmark_execution_id = $("#benchmark_execution").attr("data-id");
    var request_url = "/benchmark_executions/" + benchmark_execution_id + "/" + action + ".txt";
    $.get( request_url, function(data) {
        logTextarea.val(data);
        scrollToBottom(logTextarea);
        onSuccess();
    });
}

function liveUpdateLog(logTextarea, action) {
    var setNextTimeoutOnSuccess = function() {
        setNextLogRefreshTimeout(logTextarea, action);
    };
    updateLog(logTextarea, action, setNextTimeoutOnSuccess);
}

function initLiveLog(id, action) {
    var logTextarea = $("#" + id);
    if (logTextarea.exists()) {
        var start = $("#" + id + "RefreshStart");
        var stop  = $("#" + id + "RefreshStop");
        start.click( function() {
            start.toggle();
            stop.toggle();
            liveUpdateLog(logTextarea, action);
        });
        stop.click( function() {
            start.toggle();
            stop.toggle();
            resetAllLogRefreshTimeouts();
        });
        updateLog(logTextarea, action);
    }
}

$(document).on('ready page:load', function () {
    initLiveLog('prepareLog', 'prepare_log');
    initLiveLog('releaseResourcesLog', 'release_resources_log');
});

// Reset timeout when leaving the page via Rails turbolinks.
$(document).on('page:before-change', function() {
    resetAllLogRefreshTimeouts();
});

@@ -150,18 +150,18 @@
 eouts = 
-%5B%5D
+%7B%7D
 ;%0Afuncti
@@ -338,38 +338,39 @@
             live
-Update
+Refresh
 Log(logTextarea,
@@ -475,14 +475,19 @@
 outs
-.push(
+%5Baction%5D = 
 next
@@ -493,17 +493,16 @@
 tTimeout
-)
 ;%0A%7D%0A%0Afun
@@ -508,27 +508,24 @@
 nction reset
-All
 LogRefreshTi
@@ -533,18 +533,23 @@
 eout
-s
 (
+action
 ) %7B%0A    
 for 
@@ -548,118 +548,168 @@
     
-for (var i = 0; i %3C logRefreshTimeouts.length; i++) %7B%0A        clearTimeout(logRefreshTimeouts%5Bi%5D);
+clearTimeout(logRefreshTimeouts%5Baction%5D);%0A%7D%0A%0Afunction resetAllLogRefreshTimeouts() %7B%0A    Object.keys(logRefreshTimeouts).forEach( function(key) %7B
 %0A    
-%7D%0A
     
-l
+resetL
 ogRe
@@ -724,14 +724,21 @@
 eout
-s = %5B%5D
+(key);%0A    %7D)
 ;%0A%7D%0A
@@ -747,22 +747,23 @@
 unction 
-update
+refresh
 Log(logT
@@ -1163,30 +1163,31 @@
 unction live
-Update
+Refresh
 Log(logTexta
@@ -1310,30 +1310,31 @@
 %0A    %7D;%0A    
-update
+refresh
 Log(logTexta
@@ -1392,19 +1392,23 @@
 initLive
-Log
+Refresh
 (id, act
@@ -1689,14 +1689,15 @@
 live
-Update
+Refresh
 Log(
@@ -1827,35 +1827,32 @@
            reset
-All
 LogRefreshTimeou
@@ -1852,18 +1852,23 @@
 hTimeout
-s
 (
+action
 );%0A     
@@ -1886,14 +1886,15 @@
     
-update
+refresh
 Log(
@@ -1924,16 +1924,232 @@
    %7D%0A%7D%0A%0A
+function resetLiveRefreshButton(id) %7B%0A    var start = $(%22#%22 + id + %22RefreshStart%22);%0A    var stop  = $(%22#%22 + id + %22RefreshStop%22);%0A    if (start.is(%22:hidden%22)) %7B%0A        start.toggle();%0A        stop.toggle();%0A    %7D%0A%7D%0A%0A
 $(docume
@@ -2200,19 +2200,23 @@
 initLive
-Log
+Refresh
 ('prepar
@@ -2250,19 +2250,23 @@
 initLive
-Log
+Refresh
 ('releas
@@ -2419,24 +2419,117 @@
 unction() %7B%0A
+    resetLiveRefreshButton('prepareLog');%0A    resetLiveRefreshButton('releaseResourcesLog');%0A
     resetAll

Fix selective live refresh reset and browser back button inconsistency with turbolinks