jquery一个接一个地运行函数

jquery run functions after another

本文关键字:一个 函数 运行 jquery      更新时间:2023-09-26

我有四个函数,其中包括jQuery动画等。我希望他们在另一场比赛结束后尽快跑。我看了这么多问题和答案,我尝试了这么多事情(延期、承诺、回调等),但没有运气。通常的结果是它们是同时工作,还是第二个不运行。有什么想法吗?我该如何按顺序运行它们?

以下是我的功能:

function hides(){
  $('#whiteLogo').hide('600');
  $('div.form').hide('600');
}
function moveTop(){
  $('nav').animate({
    'bottom': 'initial',
    'top': '0px'
  }, 600);
  $('.gradient').animate({
    'bottom': 'initial',
    'top': '-10px'
  }, 600);
}
function destroys(){
  $('.gradient').hide();
}
function content(){
  $('header').after('<section id=''content''>'t<div id=''icons''>'t</div>'t<section id=''summary''>'t't't</section>'t<section id=''bulletin''>'t't</section></section>');
}

更新:我不认为这与此有关,但根据要求,这是我的HTML:

<header>
  <nav>
    <a href='#' class='linkPicture left'>
      <img src='../img/incnetWhite.png' alt='incnetWhite' id='headerLogo'>
    </a>
    <a href='../checkin/index.php' class='linkWord left' id='checkinLink'>
      Checkin
    </a>
    <a href='../weekend/index.php' class='linkWord left' id='weekendLink'>
      Weekend Departures
    </a>
    <a href='../etut/index.php' class='linkWord left' id='etutLink'>
      Etut Reservations
    </a>
    <div class='linkWord left more' id='moreLink'>
      More
      <img src='../img/header-drop.png' alt='drop' class='dropimg'>
      <div class='dropMenu more' id='moreMenu'>
        <a href='../pool/index.php' class='dropWord left' id='poolLink'>
          Pool Reservations
        </a>
        <br>
        <a href='../admin/index.php' class='dropWord left' id='adminLink'>
          Admin Tools
        </a>
      </div>
    </div>
    <div href='#' class='linkWord right personal' id='personalLink'>
      Your Name 
      <img src='../img/header-drop.png' alt='drop' class='dropimg'>
      <div class='dropMenu personal' id='personalMenu'>
        <a href='../core/settings.php' class='dropWord left' id='settingLink'>
          Change Settings
        </a>
        <br>
        <a href='../profiles/edit.php' class='dropWord left' id='profileLink'>
          Profile Settings
        </a>
        <br>
        <a href='../core/hiring.php' class='dropWord left' id='hiringLink'>
          Apply to INÇNET
        </a>
        <br>
        <a href='../core/about.php' class='dropWord left' id='aboutLink'>
          About Us
        </a>
        <br>
        <a href='../core/logoff.php' class='dropWord left' id='logoffLink'>
          Sign Out
        </a>
      </div>
    </div>
  </nav>
</header>   
<div class='form'>
    <form method='POST'>
        <span id='welcome'>Welcome!</span>
        <!--<label class='fieldname' id='username'>Username:</label>-->
        <input type='text' name='username' placeholder='username' size='10'>
        <br>
        <!--<label class='fieldname' id='password'>Password:</label>-->
        <input type='password' name='password' placeholder='&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;' size='10'>
        <br>
        <span id='remember'>
            <input type='checkbox' name='remember'>
            <label class='fildname' id='rememberme'>Remember Me</label>
        </span>
        <br>
        <span id='error'>
        </span>
        <input type='submit' name='signin' value='Sign In' id='signin'>
    </form>
</div>

据我所知,您希望按顺序对多个元素执行事件。只要稍微玩一玩,我们就可以利用jQuery的Deferred,并制定一个解决方案(查看JSFiddle),例如:

function runDeferred() {
    var args = arguments;
    if(args.length) {
        var next = Array.prototype.shift.call(args);
        $.when(next()).done(function() {
            runDeferred.apply(this, args);
        });
    }

这个方法可以获取一个方法列表,并连续调用它们。方法本身需要返回我们希望等待的元素的jQuery对象。下面是这样一个方法的例子:

function fadeBar() {
    return $("#bar").fadeIn(1000).animate({
        "font-size": "2em"
    }, 1000);
}

以下是如何组合它们:

runDeferred(fadeFoo, fadeBar, fadeZulu);

研究方法链接。这在过去对我很有效。

http://schier.co/post/method-chaining-in-javascript

回调应该可以工作。我不太确定你想做什么,但你似乎在说你想一个接一个地跑。

function hides(callback){
  $('#whiteLogo').hide('600', function(){
    $('div.form').hide('600', function(){
        if (callback){
            callback();
        }
    });
  });
}
function moveTop(callback){
  $('nav').animate({
    'bottom': 'initial',
    'top': '0px'
  }, 600, function(){
      $('.gradient').animate({
        'bottom': 'initial',
        'top': '-10px'
      }, 600, function(){
          if (callback){
              callback();
          }
      });
  });
}
function destroys(callback){
  $('.gradient').hide(function(){
      if (callback){
          callback();
      }
  });
}

//I have never used after but I'm sure it's similar to the callback structure above.
function content(){
  $('header').after('<section id=''content''>'t<div id=''icons''>'t</div>'t<section id=''summary''>'t't't</section>'t<section id=''bulletin''>'t't</section></section>');
}

回调可以这样使用。

hides(function(){
   doSomething();
});

您没有分享有关如何调用函数的信息。jQuery上的所有动画函数在完成时都有回调。要在动画完成时调用其他函数,可以使用以下代码。

function hides(){
    $('#whiteLogo').hide(600);
    $('div.form').hide(600, moveTop);
}
function moveTop(){
    $('nav').animate({
        'bottom': 'initial',
        'top': '0px'
    }, 600);
    $('.gradient').animate({
        'bottom': 'initial',
        'top': '-10px'
    }, 600, destroys);
}
function destroys(){
    $('.gradient').hide(600, content);
}
function content(){
    $('header').after('<section id=''content''>'t<div id=''icons''>'t</div>'t<section id=''summary''>'t't't</section>'t<section id=''bulletin''>'t't</section></section>');
}

这样,当你调用hide()时,你会有一个接一个的回调链。正如你所看到的,我只检查每个函数的一个动画,因为它们被限制在相同的执行时间内(Ex.hides()有两个600毫秒的动画,所以我认为如果一个已经完成,另一个也应该完成)。

请注意,这段代码可以在事件驱动模式中进行重构,如果你问我,请考虑某种更好的设计模式。

还可以查看jQuery的文档http://api.jquery.com/animate/

编辑、更新

使用.queue(),如Mottie 所建议

   function hides(){
      $('#whiteLogo').hide(600);
        $('div.form').hide(600, d);
    };
    function moveTop() {
      $('nav').animate({
        'bottom': 'initial',
        'top': '0px'
      }, 600);
      $('.gradient').animate({
        'bottom': 'initial',
        'top': '-10px'
      }, 600, d);
    };
    function destroys() {
        $('.gradient').hide(600, d);
    };
    function content() {
        $('header').after('<section style=background:blue id=''content''>'
        + ''t<div id=''icons''>'t</div>'t<section id=''summary''>'
        + ''t't't</section>'t<section id=''bulletin''>'
        + ''t't</section>section</section>');
    };
$("body").queue("animations", [hides, moveTop, destroys, content]);
var d = function() {
  return $("body").dequeue("animations")
};
hides()

http://jsfiddle.net/guest271314/0Lx4t54y/