我正在使用 jQuery mobile,我只想有一个计数器来指示在页面上花费的时间。问题是当用户没有看到页面时,我无法停止在后台递增的计数器。这是我的代码:
第 1 页
<!DOCTYPE html>
<html>
<head>
<title>Page 1</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0-beta.1/jquery.mobile-1.3.0-beta.1.min.css" />
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.0-beta.1/jquery.mobile-1.3.0-beta.1.min.js"></script>
<script>
$(document).bind("mobileinit", function(){
$.mobile.hashListeningEnabled = false;
});
var interval_id;
function checkEverySecond() {
$("#counter").html(parseInt($("#counter").html()) + 1);
};
$(document).bind("pageshow", function(){
interval_id = setInterval(function() {checkEverySecond()}, 1000);
});
$(document).bind("pagehide", function(){
clearInterval(interval_id);
});
</script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>Page 1</h1>
</div><!-- /header -->
<div data-role="content">
<p id="counter">0</p>
<a href="page2.html">Go to page 2</a>
</div><!-- /content -->
</div><!-- /page -->
</body>
</html>
第2页
<!DOCTYPE html>
<html>
<head>
<title>Page 2</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0-beta.1/jquery.mobile-1.3.0-beta.1.min.css" />
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.0-beta.1/jquery.mobile-1.3.0-beta.1.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>Page 2</h1>
</div><!-- /header -->
<div data-role="content">
<a href="page1.html">Go to page 1</a>
</div><!-- /content -->
</div><!-- /page -->
</body>
</html>
谢谢你的帮助!