HTML代码:
<body class="body" onload="buttonFunction(this)">
<form>
<p align="center"><strong>You have been on this page for </strong><input title="time spent on webpage" type="text" size="9" name="timespent"></p>
</form>
</body>
JS代码:
function buttonFunction() {
startday = new Date();
clockstart = startday.getTime();
initstopwatch();
getSecs();
}
function initstopwatch() {
var mytime = new Date();
var timeNow = mytime.getTime();
var timediff = timeNow - clockstart;
this.diffsecs = timediff/1000;
return(this.diffsecs);
}
function getSecs() {
var mySecs = initstopwatch();
var mySecs1 = ""+mySecs;
mySecs1= mySecs1.substring(0,mySecs1.indexOf("."))+ " secs. ";
document.forms[0].timespent.value = mySecs1;
window.setTimeout('getSecs()',1000);
}
现在,该功能应该用于计算用户在我的网页上的秒数,并将该变量输入到输入框中。但是,它似乎无能为力。那么这个功能有什么问题呢?
因此,让我们从头开始,因为我将能够以此方式解释更多的事情。
首先,我们需要节省用户到达页面的时间。我们可以通过在页面加载后保存日期来做到这一点。
// The variable is outside because we need every function to
// be able to access it (like a global variable)
var userArrived;
// The function to initialize the counter
function initCounter(){
// Get the date when the user arrived
// here we do not use `var` because the variable exists
userArrived = new Date().getTime(); // This returns the date in milliseconds
}
// Wait the page to load
window.addEventListener('load', function(){
// Initialize the counter
initCounter();
}, false);
现在我们需要一个函数来给我们带来不同
function getCounterValue(){
// Calculate difference
var value = new Date().getTime() - userArrived;
// This variable now have the time the user
// is on the page in milliseconds
// Now we need to return the value to the caller
return value;
}
现在我们可以获得毫秒,我们需要一个函数将其解析为人类可读的格式。
function parseMs2Sec(ms){
// We calculate seconds using seconds = milliseconds / 1000
// but we round it so that we don't have decimals
var sec = Math.round(ms/1000);
// Now we need to return the value to the caller
return sec;
}
现在唯一要做的就是每1秒(或更长时间)更新一次我们需要的任何视觉元素。
// Save the element on a variable for easy access
var timeSpent = document.forms[0].timespent;
// Update the screen with the latest time
function updateScreeenCounter(){
// Get the time the user is in the page
var ms = getCounterValue();
// Convert it to seconds
var sec = parseMs2Sec(ms);
// Display it in the page
timeSpent.value = sec + " sec.";
}
// Every 1000 milliseconds
setInterval(function(){
// Run function
updateScreeenCounter();
}, 1000);
// But this last code (with the interval)
// needs to run after the counter was initialized
// so we should put it inside the onload event we created.
这是演示中的孔代码:
//
// The variable is outside because we need every function to
// be able to access it (like a global variable)
var userArrived;
// The function to initialize the counter
function initCounter(){
// Get the date when the user arrived
// here we do not use `var` because the variable exists
userArrived = new Date().getTime(); // This returns the date in milliseconds
}
// Gives back the time since the user arrived on page (in ms)
function getCounterValue(){
// Calculate difference
var value = new Date().getTime() - userArrived;
// This variable now have the time the user
// is on the page in milliseconds
// Now we need to return the value to the caller
return value;
}
// Converts the given ms in the closest seconds
function parseMs2Sec(ms){
// We calculate seconds using seconds = milliseconds / 1000
// but we round it so that we don't have decimals
var sec = Math.round(ms/1000);
// Now we need to return the value to the caller
return sec;
}
// Update the screen with the latest time
function updateScreeenCounter(){
// Get the time the user is in the page
var ms = getCounterValue();
// Convert it to seconds
var sec = parseMs2Sec(ms);
// Display it in the page
document.forms[0].timespent.value = sec + " sec.";
}
// Wait the page to load
window.addEventListener('load', function(){
// Initialize the counter
initCounter();
// Every 1000 milliseconds
setInterval(function(){
// Run function
updateScreeenCounter();
}, 1000);
}, false);
<form>
<input name="timespent" value="Loading ..."/>
</form>
其他一些技巧:
~~~编辑~~~
我忘了提到setInterval
在这种情况下使用它会更好,因为它setTimeout
在慢速计算机中比递归更准确。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句