Guest User

Untitled

a guest
Nov 20th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. // this should all go in your header between <script> tags so that it shows on each page
  2.  
  3. // make this global so we can disable the interval on page unload
  4. var timer;
  5.  
  6. Qualtrics.SurveyEngine.addOnload(function() {
  7. function updateTime() {
  8. var start = Qualtrics.SurveyEngine.getEmbeddedData('StartTime');
  9. if (!start) {
  10. // Don't show the timer until you have set the embedded data of `StartTime`.
  11. // This way you can have an instructional page before jumping into the timed
  12. // section of the exam.
  13. document.getElementById('ClockContainer').style.display = 'none';
  14. clearInterval(timer);
  15. return;
  16. }
  17. var now = new Date().getTime();
  18. var limit = 1 * 60 * 60 * 1000; // time limit in milliseconds
  19. var elapsed = now - start;
  20. var timeLeft = (limit - elapsed) / 1000;
  21. if (timeLeft < 0) {
  22. // if the time is up we don't want the numbers to go negative so
  23. // we stop updating it and clear the interval.
  24. // You can do whatever you want in the showTimeUp function to alert
  25. // your user that the time is up
  26. showTimeUp();
  27. clearInterval(timer);
  28. return;
  29. }
  30. var displayedTime = formatTime(timeLeft);
  31. document.getElementById('Time').innerHTML = displayedTime;
  32. }
  33.  
  34. function formatTime(timeLeft) {
  35. var hoursLeft = Math.floor(timeLeft / 60 / 60);
  36. var minutesLeft = Math.floor((timeLeft % 3600) / 60);
  37. var secondsLeft = Math.floor(timeLeft % 60);
  38. if (hoursLeft < 10) {
  39. hoursLeft = "0" + hoursLeft;
  40. }
  41. if (minutesLeft < 10) {
  42. minutesLeft = "0" + minutesLeft;
  43. }
  44. if (secondsLeft < 10) {
  45. secondsLeft = "0" + secondsLeft;
  46. }
  47. var displayedTime = hoursLeft + ":" + minutesLeft + ":" + secondsLeft;
  48. }
  49.  
  50. function showTimeUp() {
  51. // choose what you want to do when the time is up. In this case
  52. // I change the clock to show in red text and send a JavaScript alert.
  53. // I also set the embedded data `Overtime` to be `true` so that I know
  54. // the user hit the time limit.
  55. document.getElementById('Time').style.color = "red";
  56. Qualtrics.SurveyEngine.setEmbeddedData('Overtime', 'true');
  57. alert("You have run out of time. Please submit what you have.");
  58. // you could click the next button for the user and have logic between blocks
  59. // to take them out of the survey if `Overtime` == `true`
  60. // jQuery('#NextButton').click();
  61. }
  62.  
  63. updateTime(); // start off the function
  64.  
  65. timer = window.setInterval(function() {
  66. updateTime();
  67. },500); // you can update it however often you want. These are milliseconds
  68. });
  69.  
  70.  
  71. Qualtrics.SurveyEngine.addOnUnload(function() {
  72. // intervals can take a lot of memory. Clear them on page turn or they'll duplicate.
  73. clearInterval(timer);
  74.  
  75. });
Add Comment
Please, Sign In to add comment