diff --git a/app/assets/javascripts/editor/editor.js.erb b/app/assets/javascripts/editor/editor.js.erb index 176596e0..511443b2 100644 --- a/app/assets/javascripts/editor/editor.js.erb +++ b/app/assets/javascripts/editor/editor.js.erb @@ -988,8 +988,8 @@ var CodeOceanEditor = { this.initializeDeadlines(); CodeOceanEditorTips.initializeEventHandlers(); - window.addEventListener("turbolinks:before-render", this.unloadAutoSave.bind(this)); - window.addEventListener("beforeunload", this.unloadAutoSave.bind(this)); + window.addEventListener("turbolinks:before-render", this.autosaveIfChanged.bind(this)); + window.addEventListener("beforeunload", this.autosaveIfChanged.bind(this)); // create autosave when the editor is opened the first time this.autosave(); } diff --git a/app/assets/javascripts/editor/submissions.js b/app/assets/javascripts/editor/submissions.js index 9afa4b27..c247ea57 100644 --- a/app/assets/javascripts/editor/submissions.js +++ b/app/assets/javascripts/editor/submissions.js @@ -204,7 +204,7 @@ CodeOceanEditorSubmissions = { const button = $(event.target) || $('#submit'); this.createSubmission(button, null, function (response) { if (response.redirect) { - this.unloadAutoSave(); + this.autosaveIfChanged(); this.editors = []; Turbolinks.clearCache(); Turbolinks.visit(response.redirect); @@ -228,13 +228,6 @@ CodeOceanEditorSubmissions = { this.autosaveTimer = setTimeout(this.autosave.bind(this), this.AUTOSAVE_INTERVAL); }, - unloadAutoSave: function() { - if(this.autosaveTimer != null){ - clearTimeout(this.autosaveTimer); - this.autosave(); - } - }, - updateSaveStateLabel: function() { var date = new Date(); var autosaveLabel = $(this.autosaveLabel); @@ -243,7 +236,15 @@ CodeOceanEditorSubmissions = { autosaveLabel.text(date.toLocaleTimeString()); }, + autosaveIfChanged: function() { + // Only save if the user has changed the code in the meantime (represented by an active timer) + if(this.autosaveTimer != null){ + this.autosave(); + } + }, + autosave: function () { + clearTimeout(this.autosaveTimer); this.autosaveTimer = null; this.createSubmission($('#autosave'), null); }