Skip to content

Commit 77ac9af

Browse files
committed
prevent conflicts on loading
1 parent 36f010f commit 77ac9af

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

js/script.js

+29-11
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,46 @@ codeEditor.on('change', function () {
3737
});
3838

3939
var cachedFile = {};
40+
var loading = false;
4041
var loadFile = function (category, algorithm, file, explanation) {
42+
if (checkLoading()) return;
4143
lastData = null;
4244
$('#explanation').html(explanation);
4345

4446
var dir = lastDir = './algorithm/' + category + '/' + algorithm + '/' + file + '/';
47+
loading = true;
4548
if (cachedFile[dir] && cachedFile[dir].data !== undefined && cachedFile[dir].code !== undefined) {
4649
dataEditor.setValue(cachedFile[dir].data, -1);
4750
codeEditor.setValue(cachedFile[dir].code, -1);
4851
} else {
4952
cachedFile[dir] = {};
5053
dataEditor.setValue('');
5154
codeEditor.setValue('');
55+
var onFail = function (jqXHR, textStatus, errorThrown) {
56+
loading = false;
57+
alert("AJAX call failed: " + textStatus + ", " + errorThrown);
58+
};
5259
$.get(dir + 'data.js', function (data) {
5360
cachedFile[dir].data = data;
5461
dataEditor.setValue(data, -1);
5562

5663
$.get(dir + 'code.js', function (code) {
5764
cachedFile[dir].code = code;
5865
codeEditor.setValue(code, -1);
59-
});
60-
}).fail(function (jqXHR, textStatus, errorThrown) {
61-
alert("AJAX call failed: " + textStatus + ", " + errorThrown);
62-
});
66+
loading = false;
67+
}).fail(onFail);
68+
}).fail(onFail);
69+
}
70+
};
71+
var checkLoading = function () {
72+
if (loading) {
73+
showErrorToast('Wait until completes loading of previous file.');
74+
return true;
6375
}
76+
return false;
6477
};
6578
var loadAlgorithm = function (category, algorithm) {
79+
if (checkLoading()) return;
6680
$('#list > button').removeClass('active');
6781
$('[data-category="' + category + '"][data-algorithm="' + algorithm + '"]').addClass('active');
6882
$('#btn_desc').click();
@@ -167,6 +181,16 @@ $('#navigation').click(function () {
167181
_tracer.resize();
168182
});
169183

184+
var showErrorToast = function (err) {
185+
var $toast = $('<div class="toast error">').append(err);
186+
$('.toast_container').append($toast);
187+
setTimeout(function () {
188+
$toast.fadeOut(function () {
189+
$toast.remove();
190+
});
191+
}, 3000);
192+
};
193+
170194
$('#btn_run').click(function () {
171195
try {
172196
eval(dataEditor.getValue());
@@ -177,13 +201,7 @@ $('#btn_run').click(function () {
177201
_tracer.visualize();
178202
} catch (err) {
179203
console.error(err);
180-
var $toast = $('<div class="toast error">').append(err);
181-
$('.toast_container').append($toast);
182-
setTimeout(function () {
183-
$toast.fadeOut(function () {
184-
$toast.remove();
185-
});
186-
}, 3000);
204+
showErrorToast(err);
187205
}
188206
});
189207
$('#btn_pause').click(function () {

0 commit comments

Comments
 (0)