English 中文(简体)
JQuery 流动中未等待特定间隔的时间
原标题:setTimeout not waiting for the specified interval in JQuery Mobile

I am trying to change pages based on specific time interval. I tried using setTimeout, but it run the code immediately regardless the specified time. Here is the page:

<!DOCTYPE HTML>
<html>
    <head>
        <meta name="viewport" content="width=320; user-scalable=no" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <title>Change Page</title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
        <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script type="text/javascript">
            var oCSS = {
                 font-size  :  18em ,
                 height  :  300px ,
                 border  :  thick solid ,
                 text-align  :  center 
            };

            $(document).bind("mobileinit", function()
            {
                $.mobile.defaultPageTransition = "flip";
            });
            $( div[data-role|="page"] ).live( pageshow , function(event)
            {
                setTimeout($.mobile.changePage($($(this).attr( NextPage ))), 30000);
            });
        </script>

        <script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
    </head>
<body>
    <div data-role="page" id="page1" NextPage= #page2 >
        <div data-role="content">
            <div class="number">4</div>
        </div>
    </div>

    <div data-role="page" id="page2" NextPage= #page3 >
        <div data-role="content">
            <div class= number >3</div>
        </div>
    </div>

    <div data-role="page" id="page3" NextPage= #page4 >
        <div data-role="content">
            <div class= number >2</div>
        </div>
    </div>

    <div data-role="page" id="page4" NextPage= #page1 >
        <div data-role="content">
            <div class= number >1</div>
        </div>
    </div>

    <script type="text/javascript">
        $(".number").css(oCSS);
    </script>
</body>
</html>
最佳回答

你的辛子是不正确的。 您需要使用匿名功能,或将立即打电话。 此外,贵格文似乎也不正确(太多<代码>$()。 应当:

$( div[data-role|="page"] ).live( pageshow , function(event)
{
    // Retrieve attribute of element to be passed to anonymous function
    var functionParam = $(this).attr( NextPage )

    setTimeout(function() {
        // Pass functionParam to function - $(this) will 
        // be out of scope when the function is called
        $.mobile.changePage(functionParam)
    }, 30000);​
});

See window.settimeoutdocs for further information.

问题回答

您对<代码>$(原文)的提及将不可行(匿名功能中的这一内容最终为DOMWindow<<<>http://jsfiddle.net/marklemanco/t8gfw/"rel=“nofollow”>Example)。

$( div[data-role|="page"] ).live( pageshow , function(event){
  var nextPage = $($(this).attr( NextPage ));
  setTimeout(function(){
    $.mobile.changePage(nextPage);
  }, 30000);
});

Here is a working example on jsfiddle





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.