我在javaScript时机上有一些问题。从焦点上,我希望它在焦点不集中时开始计时,而当它恢复到焦点时,时机将停止并且会显示。
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
}
});
现在,Istimeron变量是一个标志,它将在以下时期设置:
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');
}
}
此功能ProcessThissearch(表单)将以以下HTML表格调用:
<form align=right action="mainheader.jsp" method="POST"><input type="text" name="search"><input type="submit" value="Open Website" onClick="ProcessThisSearch(this.form)"></form>
我相信问题在于iStimeron变量。因为我已经测试了两个事件侦听器,并且它正在工作。只有当我添加istimeron变量时,它似乎不起作用。
您的HTML代码将设置IsTimeron = true,仅在表单上提交。可能这不是您想要的。
提交表单还将将当前页面更改为mainheader.jsp,并通过ProcessThissearch函数加载另一个页面。
可能的修复将是:
<button onClick="ProcessThisSearch(this.form)">Open Website</button>
或
<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>