JavaScript to start and stop timing
<p>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.</p>

<pre><code>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

}
});
</code></pre>

<p>Now the isTimerOn variable is a flag which will be set when:</p>

<pre><code>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');
    }

}
</code></pre>

<p>This function ProcessThisSearch(form) will be invoked in the following HTML form:</p>

<pre><code>&lt;form align=right action="mainheader.jsp" method="POST"&gt;&lt;input type="text" name="search"&gt;&lt;input type="submit" value="Open Website" onClick="ProcessThisSearch(this.form)"&gt;&lt;/form&gt;
</code></pre>

<p>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.</p>
