I have some problems with JavaScript timings..Basically i want it to start timing when it goes out of focus and when it comes back to focus, the timing will stop and it will display.
var start,end,isTimerOn=0;
window.addEventListener('blur', function() { //when user get out of the screen
/*Start Time when user goes out of focus*/
start = new Date().getTime();
});
window.addEventListener('focus', function() { //when user focus on the screen
if (isTimerOn==1)
{
end = new Date().getTime();
var time = end - start; //time will be in ms. eg: 1 sec will be 1000
/*Convert to seconds*/
var y=Math.round(time/1000);
start=0; //reset
isTimerOn=0; //reset
alert('Execution time: ' + y + 'secs'); //this will print the time how long the user has been away
}
});
Now the isTimerOn variable is a flag which will be set when:
function ProcessThisSearch(form)
{
//alert("OI!"); //test is js is working.
var test=form.search.value;
//alert(test); //test if value can be retrieved
if (test)
{
isTimerOn=1;
window.open('http://www.'+test+'.com');
}
}
This function ProcessThisSearch(form) will be invoked in the following HTML form:
<form align=right action="mainheader.jsp" method="POST"><input type="text" name="search"><input type="submit" value="Open Website" onClick="ProcessThisSearch(this.form)"></form>
I believe the problem is with the isTimerOn variable. Because I have tested the two event listener and it is working. Only when I add isTimerOn variable, it doesn’t seem to work.
Your HTML code will set isTimerOn=true, only on form submit.
Probably this is NOT what you want.
Submitting the form will also change the current page to mainheader.jsp AND load another page by the ProcessThisSearch function.
The possible fixes would be:
<button onClick="ProcessThisSearch(this.form)">Open Website</button>OR
<form align=right action="mainheader.jsp" method="POST"><input type="text" name="search"><input type="submit" value="Open Website" onClick="ProcessThisSearch(this.form);return false;"></form>