0

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.

6
  • 1
    Typo on line 2 of code indow. should be window.
    – Scott
    Oct 25, 2012 at 12:42
  • Hi, I have checked the actual code. The actual code has no typo. I accidentally made a typo while copy and pasting in Stack Overflow.
    – Ferrino
    Oct 25, 2012 at 13:02
  • Have You considered the setInterval function for timing?
    – undefined
    Oct 25, 2012 at 13:06
  • What exactly is the problem with the isTimerOn variable, what does not work?
    – Bergi
    Oct 25, 2012 at 13:06
  • 1
    @xyu: How would that help? Apart from the fact it is highly unreliable, in comparison to Date.
    – Bergi
    Oct 25, 2012 at 13:07

1 Answer 1

2

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>

2
  • Hello anishsane, thank you very much for your help. The solution seems to work. Just a few question for my understanding: How does the return false help in the form?
    – Ferrino
    Oct 25, 2012 at 13:17
  • in general, "return false" on any onclick event will abort the normal effect of the click. e.g. for 'a' elements, it will abort link navigation, for form's input type=submit element, it will abort form submission. The code in onclick (ProcessThisSearch in your example) will however get executed.
    – anishsane
    Nov 12, 2012 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.