javascript jquery推迟

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript jquery推迟相关的知识,希望对你有一定的参考价值。

/*

  var dfd = $.Deferred();
  return dfd.promise();

  var deferred = $.Deferred();
  
  deferred.resolve();
  
  return deferred.promise();

  deferred.reject();

*/


function getLatestNews() {
    return $.get( "latestNews.php", function( data ) {
        console.log( "news data received" );
        $( ".news" ).html( data );
    });
}
 
function getLatestReactions() {
    return $.get( "latestReactions.php", function( data ) {
        console.log( "reactions data received" );
        $( ".reactions" ).html( data );
    });
}
 
function showAjaxedContent() {
    // The .promise() is resolved *once*, after all animations complete
    return $( ".news, .reactions" ).slideDown( 500, function() {
        // Called once *for each element* when animation completes
        $(this).addClass( "active" );
    }).promise();
}
 
function removeActiveClass() {
    return $.Deferred(function( dfd ) {
        setTimeout(function () {
            $( ".news, .reactions" ).removeClass( "active" );
            dfd.resolve();
        }, 4000);
    }).promise();
}
 
$.when(
    getLatestNews(),
    getLatestReactions()
)
.then(showAjaxedContent)
.then(removeActiveClass)
.then(function() {
    console.log( "Requests succeeded and animations completed" );
}).fail(function() {
    console.log( "something went wrong!" );
});

以上是关于javascript jquery推迟的主要内容,如果未能解决你的问题,请参考以下文章

html 推荐的例子。 jQuery推迟了

php POC推迟需要jQuery的内联JS

php 使用HTML5 defer属性推迟jQuery解析

javascript 推迟加载javascript

javascript 推迟脚本

php 推迟解析JavaScript