I am looking to have a countdown timer (much like the booking agency websites like ticketek use) so the user knows that we will hold a reservation for 10 minutes, and they better hurry up and fill in the form!
I have a gap in my knowledge with how I can incorporate other javascript into an xPage. I would appreciate any assistance.
Essentially my plan is as follows:
- Load the page.
- Have a countdown timer run from 10:00 to 0:00
- At 0:00, go to the new page and lose all data entered
To do this, I referenced the top answer in this stackoverflow post (The simplest possible JavaScript countdown timer?) and am trying to use it in the xPage.
I have incorporated this code into an xPage (see below) as is (so it does not work) hoping it would work, and to then change it to use a computed field control. I have left it as is hoping someone can direct me to where my understanding of this is wrong?
Any pointers as to where I am going wrong, or how best to get a control like this working?
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:this.afterPageLoad>
<xp:executeScript>
<xp:this.script><![CDATA[#{javascript:function startTimer(duration, display) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
var fiveMinutes = 60 * 5,
display = document.querySelector('#time');
startTimer(fiveMinutes, display);}]]></xp:this.script>
</xp:executeScript>
</xp:this.afterPageLoad>
<div>
Please complete in <span id="time">05:00</span>minutes.
</div>
</xp:view>