forms: let autosave() call finish before starting a new one

This commit is contained in:
Frédéric Péters 2016-01-20 14:28:50 +01:00
parent 5fa59bdcc4
commit 63c850e1cb
1 changed files with 11 additions and 3 deletions

View File

@ -5,9 +5,13 @@ $(function() {
});
if ($('form[data-has-draft]').length == 1) {
var last_auto_save = $('form[data-has-draft]').serialize();
var timeout_id = window.setInterval(function() {
var timeout_id = null;
function autosave() {
var new_auto_save = $('form[data-has-draft]').serialize();
if (last_auto_save == new_auto_save) return;
if (last_auto_save == new_auto_save) {
timeout_id = window.setTimeout(autosave, 5000);
return;
}
$.ajax({
type: 'POST',
url: window.location.pathname + 'autosave',
@ -16,9 +20,13 @@ $(function() {
if (json.result == 'success') {
last_auto_save = new_auto_save;
}
},
complete: function() {
timeout_id = window.setTimeout(autosave, 5000);
}
});
}, 5000);
}
timeout_id = window.setTimeout(autosave, 5000);
}
$('#tracking-code button').click(function() {
$('input[name=savedraft]').click();