It’s not often when you’d like to use a JavaScript timer to close a browser window once the timer expires/ = 0.0 seconds.
I found this code snippet on the web and modified it slightly to my needs, I hope this benefits you. This code snippet has helped me provide plentiful solutions to our clients, I’m certain you will find this blog of some use if you substantially deal with time and dates.
1.) Start by embedding this JavaScript into either a module JavaScript section or some kind of Script Injector module.
THE SCRIPT:
----------------------------------
//START
var milisec= 0; // <—Declaring Variable and assigning it.
var seconds=15; // <—Declaring Variable and assigning it.
document.getElementById('d2').innerHTML ='15'; // <—Assigning my span tag the inner HTML value of 15
display() ; //Calling the display() function in which I have defined below.
/*
Purpose of the display Function:
1.) Count down time starting at 15 seconds all the way until 0.0 seconds. Once 0.0 seconds is reached I am redirecting the page to which ever page is indexed in the browser history as the page was visiting prior to this page.
1a.) As you can tell, I am updating the current seconds to the span tag that I have specified, named “d2”
*/
function display(){
if (milisec<=0){
milisec=9;
seconds-=1;
}
if (seconds<=-1){
milisec=0;
seconds+=1;
}
else
{
milisec-=1;
document.getElementById('d2').innerHTML =seconds+"."+milisec;
setTimeout("display()",100);
}
if(document.getElementById('d2').innerHTML == '0.0')
{
window.history.go(-1);
}
}
// END
I find this script to be very useful. Anything dealing with time or dates can be tricky. But if it’s well explained then you can benefit from the programming logic behind the code and mutate it to your needs.