Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this should all go in your header between <script> tags so that it shows on each page
- // make this global so we can disable the interval on page unload
- var timer;
- Qualtrics.SurveyEngine.addOnload(function() {
- function updateTime() {
- var start = Qualtrics.SurveyEngine.getEmbeddedData('StartTime');
- if (!start) {
- // Don't show the timer until you have set the embedded data of `StartTime`.
- // This way you can have an instructional page before jumping into the timed
- // section of the exam.
- document.getElementById('ClockContainer').style.display = 'none';
- clearInterval(timer);
- return;
- }
- var now = new Date().getTime();
- var limit = 1 * 60 * 60 * 1000; // time limit in milliseconds
- var elapsed = now - start;
- var timeLeft = (limit - elapsed) / 1000;
- if (timeLeft < 0) {
- // if the time is up we don't want the numbers to go negative so
- // we stop updating it and clear the interval.
- // You can do whatever you want in the showTimeUp function to alert
- // your user that the time is up
- showTimeUp();
- clearInterval(timer);
- return;
- }
- var displayedTime = formatTime(timeLeft);
- document.getElementById('Time').innerHTML = displayedTime;
- }
- function formatTime(timeLeft) {
- var hoursLeft = Math.floor(timeLeft / 60 / 60);
- var minutesLeft = Math.floor((timeLeft % 3600) / 60);
- var secondsLeft = Math.floor(timeLeft % 60);
- if (hoursLeft < 10) {
- hoursLeft = "0" + hoursLeft;
- }
- if (minutesLeft < 10) {
- minutesLeft = "0" + minutesLeft;
- }
- if (secondsLeft < 10) {
- secondsLeft = "0" + secondsLeft;
- }
- var displayedTime = hoursLeft + ":" + minutesLeft + ":" + secondsLeft;
- }
- function showTimeUp() {
- // choose what you want to do when the time is up. In this case
- // I change the clock to show in red text and send a JavaScript alert.
- // I also set the embedded data `Overtime` to be `true` so that I know
- // the user hit the time limit.
- document.getElementById('Time').style.color = "red";
- Qualtrics.SurveyEngine.setEmbeddedData('Overtime', 'true');
- alert("You have run out of time. Please submit what you have.");
- // you could click the next button for the user and have logic between blocks
- // to take them out of the survey if `Overtime` == `true`
- // jQuery('#NextButton').click();
- }
- updateTime(); // start off the function
- timer = window.setInterval(function() {
- updateTime();
- },500); // you can update it however often you want. These are milliseconds
- });
- Qualtrics.SurveyEngine.addOnUnload(function() {
- // intervals can take a lot of memory. Clear them on page turn or they'll duplicate.
- clearInterval(timer);
- });
Add Comment
Please, Sign In to add comment