Hello, I am trying to set-up a timer that will be initiated upon starting a certain block within a survey. The timer should appear on the top of the screen regardless of scrolling and continue to count down as the participant clicks through different pages within the block. Upon the timer reaching 0, I want the survey to progress to a final question (a pseudo-end screen, it can be in the same block or in its own block if that helps) and upon this question loading the timer would no longer be visible.
I have very little programming experience so I am mostly just grabbing code I can find online and trying to adapt it. The code I have found is able to make a timer appear when the block starts and remain on screen despite scrolling but upon reaching 0 it just displays text and doesn't advance anything. It also persists at the top of the screen when at my "pseudo-end screen" as well as when the actual survey termination end screen appears.
The following code is added onto the first question of the block where I want it to start:
Qualtrics.SurveyEngine.addOnload(function()
{
var headerCont = document.createElement("div");
headerCont.className = "header-cont";
headerCont.id = "header_container";
var header = document.createElement("div");
header.className = "header"
header.id = "header_1";
var timer = document.createElement("div");
timer.className = "timer";
timer.id = "timer_1";
timer.innerHTML = "Time Remaining: 07:30";
headerCont.appendChild(header);
header.appendChild(timer);
document.body.insertBefore(headerCont, document.body.firstChild);
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
var myTimer = setInterval(function() {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
var text = ('innerText' in display)? 'innerText' : 'textContent';
display[text] = minutes + ":" + seconds;
if (--timer < 0) {
clearInterval(myTimer);
timeOver();
}
}, 1000);
}
var timerSeconds = 450,
display = document.querySelector('#time');
startTimer(timerSeconds, display);
var timeOver = function() {
document.getElementById("timer_1").innerHTML = "Time is up.";
x = 1;
var bgColor = setInterval(change, 1000);
}
});
I also have the following custom CSS code under Style in "Look and Feel"
.header-cont {
width:100%;
position:fixed;
top:0px;
z-index:1000;
}
.header {
height:auto;
background:#FFFFFF;
width:100%;
top: 0px;
margin:0px auto;
position:fixed;
z-index:1000;
}
.timer{
width: 500px;
margin: auto;
text-align: center;
vertical-align: middle;
line-height: 50px;
font-size: 28px;
}
Thank you for any help you can provide.
Question
Problems with Javascript timer
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
