app-test.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. $(function() {
  2. initialiseTestForm();
  3. });
  4. function initialiseTestForm() {
  5. var date = new Date();
  6. var testStartTime = date.getTime();
  7. $form = $("#test-form");
  8. $stopButtonContainer = $("#test-stop-button-container");
  9. $stopButton = $("#test-stop-button");
  10. $submitButtonContainer = $("#test-submit-button-container");
  11. $submitButton = $("#test-submit-button");
  12. $noInternetText = $("#test-no-internet-text");
  13. $submitStatus = $("#test-submit-status");
  14. $stopButton.on("click", function(event) {
  15. event.preventDefault();
  16. var date = new Date();
  17. var testEndTime = date.getTime();
  18. var testDuration = testEndTime - testStartTime;
  19. $("#duration").val(testDuration);
  20. $stopButtonContainer.addClass("d-none");
  21. $submitButtonContainer.removeClass("d-none");
  22. });
  23. $form.on("submit", function(event) {
  24. event.preventDefault();
  25. $noInternetText.addClass("d-none");
  26. $submitStatus.html("Submitting...");
  27. $submitButton.attr("disabled", "");
  28. var data = $form.serialize();
  29. $.ajax({
  30. type: "post",
  31. url: window.location,
  32. data: data,
  33. success: function(data, text) {
  34. try {
  35. var data = $.parseJSON(data);
  36. if(!data.success) throw "Unsuccessful.";
  37. $submitStatus.html("Submitted. Thank you for participating in this test. You will be redirected to the Tests page in three seconds.");
  38. setTimeout(function() {
  39. window.location = "/account/tests";
  40. }, 3000);
  41. }
  42. catch(err) {
  43. $submitStatus.html("Error, please try again later.");
  44. $submitButton.removeAttr("disabled");
  45. }
  46. console.log(data);
  47. },
  48. error: function(request, status, error) {
  49. console.log(request, status, error);
  50. $submitStatus.html("");
  51. $noInternetText.removeClass("d-none");
  52. $submitButton.removeAttr("disabled");
  53. }
  54. });
  55. });
  56. }