Javascript / JQuery:需要时间延迟

时间:2022-04-21 01:17:13

I am trying to implement a jquery function that displays the age of a person. The code for this is at http://keith-wood.name/countdown.html

我正在尝试实现一个显示一个人年龄的jquery函数。此代码位于http://keith-wood.name/countdown.html

What I have so far works, if I uncomment the alert function, but not with out it. I have tried the javascript setTimeout function, but it does not wait and executes straight away. If I run it with setTimeout and the alert, the popup pops up straight away.

到目前为止我所做的工作,如果我取消注释警报功能,但没有取消它。我已经尝试了javascript setTimeout函数,但它不会等待并立即执行。如果我使用setTimeout和警报运行它,弹出窗口会立即弹出。

How and where do I implement the delay, so that the html element is loaded before executing the code that fills it.

我如何以及在何处实现延迟,以便在执行填充它的代码之前加载html元素。

function myAgeLoaded( ) {
    var austDay = new Date();
    austDay = new Date( 1960, 7-1, 18 );
    $( "#defaultCountdown" ).countdown({since: austDay, format: 'YOWDHMS'}).delay( 1500 );
}

function display_text( which )
{

    $.post( "display.php", { content_changed : 1, which : which }, function( data ){ document.getElementById( "main_text" ).innerHTML = html =  data }, "html" );

    if( which == 'abt' ){
//      alert( which );
        myAgeLoaded();
    }

    return false;
}

1 个解决方案

#1


2  

The alert will works because it postponed the code execution, so the post my receive the data before you clicked out the pop out, which act as delay, however, you the internet is too slow, or you clicked it out too fast, it may not work.

警报将起作用,因为它推迟了代码执行,所以在你点击弹出窗口之前收到数据的帖子,这可以作为延迟,但是,你上网太慢,或者你点击它太快,它可能不行。

And from jQuery.post(), you can see the 3rd parameter you passed in, is a function, which will be executed after the request is success, you should also execute myAgeLoaded after you insert the data into your page. So myAgeLoaded should guaranteed to see the #defaultCountdown after then.

从jQuery.post()中,您可以看到传入的第三个参数,是一个函数,它将在请求成功后执行,您还应该在将数据插入页面后执行myAgeLoaded。所以myAgeLoaded应该保证在那之后看到#defaultCountdown。

function myAgeLoaded( ) {
    var austDay = new Date();
    austDay = new Date( 1960, 7-1, 18 );
    $( "#defaultCountdown" ).countdown({since: austDay, format: 'YOWDHMS'}).delay( 1500 );
}

function display_text( which ) {

    $.post("display.php", {
          content_changed : 1, 
          which : which
          }, 
          // This is the success callback, it'll be called when the request is
           // response with success code and data.
          function( data ) { 
              document.getElementById( "main_text" ).innerHTML = html =  data;
              // Now the page should be filled with content, call the func now.
              if (which === 'abt') {
                myAgeLoaded();
              }
              
          }, "html");

    return false;
}

#1


2  

The alert will works because it postponed the code execution, so the post my receive the data before you clicked out the pop out, which act as delay, however, you the internet is too slow, or you clicked it out too fast, it may not work.

警报将起作用,因为它推迟了代码执行,所以在你点击弹出窗口之前收到数据的帖子,这可以作为延迟,但是,你上网太慢,或者你点击它太快,它可能不行。

And from jQuery.post(), you can see the 3rd parameter you passed in, is a function, which will be executed after the request is success, you should also execute myAgeLoaded after you insert the data into your page. So myAgeLoaded should guaranteed to see the #defaultCountdown after then.

从jQuery.post()中,您可以看到传入的第三个参数,是一个函数,它将在请求成功后执行,您还应该在将数据插入页面后执行myAgeLoaded。所以myAgeLoaded应该保证在那之后看到#defaultCountdown。

function myAgeLoaded( ) {
    var austDay = new Date();
    austDay = new Date( 1960, 7-1, 18 );
    $( "#defaultCountdown" ).countdown({since: austDay, format: 'YOWDHMS'}).delay( 1500 );
}

function display_text( which ) {

    $.post("display.php", {
          content_changed : 1, 
          which : which
          }, 
          // This is the success callback, it'll be called when the request is
           // response with success code and data.
          function( data ) { 
              document.getElementById( "main_text" ).innerHTML = html =  data;
              // Now the page should be filled with content, call the func now.
              if (which === 'abt') {
                myAgeLoaded();
              }
              
          }, "html");

    return false;
}