HTML code:
<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);
}
现在,此函数应该计算用户在我的网页上的秒数,并将该变量输入到输入框中。但是,它似乎什么都不做。那么这个功能有什么问题?
答案 0 :(得分:1)
Per Thanasis Grammatopoulos的评论,我之前的回答(如下)是错误的。我尝试通过修复分号positioining来运行代码,并在Safari中运行。
window.setTimeout('getSecs()',1000; )
应该是
window.setTimeout('getSecs()',1000);
我之前的错误答案: setTimeout只调用一次getSecs()。我想你想每秒调用一次,而不是一次 in 一次,在这种情况下你应该使用:
window.setInterval(getSecs,1000);
如果你想稍后停止间隔(可能是个好主意),你可以这样做:
var interval = window.setInterval(getSecs,1000);
稍后当您想要停止计时器时,只需致电:
clearInterval(interval);
答案 1 :(得分:0)
基本上,setTimeout
应替换为setInterval
(因为您希望getSecs
重复调用而不只是一次)。然后,您希望传递给它的是对函数的引用而不是它的调用,因此getSecs
(没有引号或括号)而不是"getSecs()"
。而这很可能就是原因。我现在无法测试代码。但问题是getSecs()
不应该调用自己,因为它将由setInterval
其次,代码值得进行大规模的清理,但如果没有人提出一个好的重构,我明天就能提供更多的帮助。
答案 2 :(得分:0)
所以,让我们从头开始,因为我将能够以这种方式解释更多的东西。
首先,我们需要节省用户到达页面的时间。 我们可以通过在页面加载后保存日期来完成此操作。
// 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);
&#13;
<form>
<input name="timespent" value="Loading ..."/>
</form>
&#13;
更多提示: