2

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>

ご協力いただきありがとうございます!

4

2 に答える 2

1

コードを変更する必要があります。jQmページのイベントの動作は少し異なります。ページイベントをすべてのページにバインドしていましたが、これは問題でした。それらを単一のページにバインドする必要があります。

実例は次のとおりです:http://jsfiddle.net/Gajotres/DU493/

    var timerObject = {
        interval_id : null
    }

    function checkEverySecond() {
        $("#counter").html(parseInt($("#counter").html()) + 1);
    };        

    $(document).on('pagebeforeshow', '#page1', function(){   
            timerObject.interval_id = setInterval(function() {checkEverySecond()}, 1000);    
    }); 

    $(document).on('pagehide', '#page1', function(){   
            clearInterval(timerObject.interval_id);  
    }); 

私の例は1つのhtmlマルチページテンプレートですが、同じことが複数のhtmlファイルでも同じように機能します。

編集 :

マルチHTMLの例で機能しなかった理由は、onの代わりbindが使用されたためです。また、jQuery Mobileをロードする前に、mobileinitを宣言する必要があります。

于 2013-02-07T10:29:50.787 に答える
0

ウィンドウにフォーカスがあるときにインクリメントできます。

var window_focus;

$(window).focus(function() {
    window_focus = true;
})

while (window_focus) {
    checkEverySecond()
}  

JavaScript / jQuery:ウィンドウにフォーカスがあるかどうかをテストします

于 2013-02-07T09:54:39.623 に答える