diff options
author | prathamesh | 2017-09-08 13:41:40 +0530 |
---|---|---|
committer | prathamesh | 2017-09-08 14:13:52 +0530 |
commit | ad020f9039e33fc6c29e9f0e1de8fd56c2f78406 (patch) | |
tree | 5a97db6d45a5da9d4fa5e0d6decc93a216ae09f8 /yaksh/static | |
parent | 09c00608a215df7a44782ab3f7c97cb912664bfd (diff) | |
download | online_test-ad020f9039e33fc6c29e9f0e1de8fd56c2f78406.tar.gz online_test-ad020f9039e33fc6c29e9f0e1de8fd56c2f78406.tar.bz2 online_test-ad020f9039e33fc6c29e9f0e1de8fd56c2f78406.zip |
Modification as per comments on the PR
Diffstat (limited to 'yaksh/static')
-rw-r--r-- | yaksh/static/yaksh/js/requesthandler.js | 92 |
1 files changed, 41 insertions, 51 deletions
diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index 9e2c2e5..73d0405 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -1,34 +1,34 @@ -request_status = "initial" +request_status = "initial"; count = 0; -checker = null + function submitRequest(){ document.forms["code"].submit(); } +function reset_values() { + request_status = "initial"; + count = 0; +} function check_state(state, uid) { if ((state == "running" || state == "not started") && count < 7) { count++; setTimeout(function() {get_result(uid);}, 2000); } else if (state == "unknown") { - request_status = "initial"; - count = 0; + reset_values(); notify("Request timeout. Try again later"); - clearInterval(checker); unlock_screen(); } else { - request_status = "initial"; - count = 0; - notify("Please try after few minutes"); - clearInterval(checker); + reset_values() + notify("Please try again"); unlock_screen(); } } function notify(text) { - var $notice = document.getElementById("notification"); - $notice.classList.add("alert"); - $notice.classList.add("alert-success"); - $notice.innerHTML = text; + var notice = document.getElementById("notification"); + notice.classList.add("alert"); + notice.classList.add("alert-success"); + notice.innerHTML = text; } function lock_screen() { @@ -39,54 +39,45 @@ function unlock_screen() { document.getElementById("ontop").style.display = "none"; } -function check_lock_screen() { - var $ontop_div = document.getElementById("ontop"); - if ($ontop_div.style.display == "block") { - $ontop_div.style.display = "none"; - } +function get_result(uid){ + var url = "/exam/get_result/"+uid+"/"; + ajax_check_code(url, "GET", "html", null, uid) } -function get_result(uid){ - var url = "/exam/get_results/"+uid+"/"; - ajax_call(url, "GET", "html", null, uid) +function response_handler(method_type, content_type, data, uid){ + if(content_type.indexOf("text/html") !== -1) { + if( method_type === "POST") { + reset_values(); + } + unlock_screen(); + document.open(); + document.write(data); + document.close(); + } else if(content_type.indexOf("application/json") !== -1) { + res = JSON.parse(data); + request_status = res.status; + if(method_type === "POST") { + uid = res.uid; + } + check_state(request_status, uid); + } else { + reset_values(); + unlock_screen(); + } } -function ajax_call(url, method_type, data_type, data, uid) { +function ajax_check_code(url, method_type, data_type, data, uid) { $.ajax({ method: method_type, url: url, data: data, - dataType: data_type, // Your server can response html, json, xml format. + dataType: data_type, success: function(data, status, xhr) { content_type = xhr.getResponseHeader("content-type"); - if(content_type.indexOf("text/html") !== -1) { - if( method_type === "POST") { - request_status = "initial"; - count = 0; - } - clearInterval(checker); - unlock_screen(); - document.open(); - document.write(data); - document.close(); - } else if(content_type.indexOf("application/json") !== -1) { - res = JSON.parse(data); - request_status = res.status; - if(method_type === "POST") { - uid = res.uid; - } - check_state(request_status, uid); - } else { - request_status = "initial"; - count = 0; - clearInterval(checker); - unlock_screen(); - } + response_handler(method_type, content_type, data, uid) }, error: function(xhr, text_status, error_thrown ) { - request_status = "initial"; - count = 0; - clearInterval(checker); + reset_values(); unlock_screen(); notify("There is some problem. Try later.") } @@ -130,10 +121,9 @@ $(document).ready(function(){ global_editor.editor.clearHistory(); } $('#code').submit(function(e) { - checker = setInterval(check_lock_screen, 30000); lock_screen(); var data = $(this).serializeArray(); - ajax_call($(this).attr("action"), "POST", "html", data, null) + ajax_check_code($(this).attr("action"), "POST", "html", data, null) e.preventDefault(); // To stop the default form submission. }); }); |