我正在尝试编写一个脚本,我需要一个计时器来计算弹出窗口打开的秒数。我是编程新手,但我假设你使用javascript吗?
答案 0 :(得分:5)
在弹出窗口中,您可以使用unload
对象的window
事件来检测窗口关闭或导航到新页面,之前已记录它在顶部打开的时间该文件。例如:
<html>
<head>
<script type="text/javascript">
var start = new Date();
window.onunload = function() {
var end = new Date();
var secondsOpen = Math.floor((end - start) / 1000);
alert("Pop-up was open for " + secondsOpen + " seconds");
};
</script>
</head>
<body>
...
</body>
</html>
答案 1 :(得分:1)
当计算某个时间范围时,我总是使用日期对象,在弹出窗口打开时将新日期存储在变量中,并将该值减去当前日期。这是一个例子:
// Execute this when the popup opens
var popup_opened = (new Date()).getTime();
// And this way you can get the time (in seconds) that the popup has been opened
var current_time = (new Date()).getTime();
var time_spent_opened = (current_time - popup_opened)/100;
您还可以使用以下功能检索弹出窗口多次打开的时间:
function getPopupTime() {
var current_time = (new Date()).getTime();
return (current_time - popup_opened)/100;
}