【问题标题】:jQuery: Make second function wait to execute, until first function is completejQuery:让第二个函数等待执行,直到第一个函数完成
【发布时间】:2019-02-16 11:47:33
【问题描述】:

我正在尝试使用whendone 使第二个函数仅在第一个函数执行后才运行,但它不起作用。 我想在这里实现的是:

首先我使用$("#jobshome").load("jobs/newest-jobs .js-toprow");从外部页面加载一个div(在同一个域中,这里没有跨域),然后在它完全加载后,下一个脚本,即一个简单的文本滑块,将运行。 但是现在外部 HTML 不再加载到“#jobshome”中了。

当我不使用这种方法时,我得到的结果是整个外部 div 将加载到单个滑块中,这不是我需要的。

如果我像这样使用初始部分,它可以工作,但不像预期的那样:

$(function(){$("#jobshome").load("jobs/newest-jobs .js-toprow");});
        $(function(){

    //rotation speed and timer

我的代码如下:

jQuery(document).ready(function($) {

  $.when(function() {
    $("#jobshome").load("jobs/newest-jobs .js-toprow");
  }).done(function() {

    //rotation speed and timer
    var speed = 3000;
    var run = setInterval(rotate, speed);
    var slides = $('.js-toprow');
    var container = $('#jobshome');
    var elm = container.find(':first-child').prop("tagName");
    var item_height = container.height();
    var previous = 'prevabc'; //id of previous button
    var next = 'nextabc'; //id of next button
    slides.height(item_height); //set the slides to the correct pixel height
    container.parent().height(item_height);
    container.height(slides.length * item_height); //set the slides container to the correct total height
    container.find(elm + ':first').before(container.find(elm + ':last'));
    resetSlides();


    //if user clicked on prev button

    $('#buttonsabc a').click(function(e) {
      //slide the item

      if (container.is(':animated')) {
        return false;
      }
      if (e.target.id == previous) {
        container.stop().animate({
          'top': 0
        }, 1500, function() {
          container.find(elm + ':first').before(container.find(elm + ':last'));
          resetSlides();
        });
      }

      if (e.target.id == next) {
        container.stop().animate({
          'top': item_height * -2
        }, 1500, function() {
          container.find(elm + ':last').after(container.find(elm + ':first'));
          resetSlides();
        });
      }
      //cancel the link behavior            
      return false;

    });

    //if mouse hover, pause the auto rotation, otherwise rotate it    
    container.parent().mouseenter(function() {
      clearInterval(run);
    }).mouseleave(function() {
      run = setInterval(rotate, speed);
    });


    function resetSlides() {
      //and adjust the container so current is in the frame
      container.css({
        'top': -1 * item_height
      });
    }

  });
});
//a simple function to click next link
//a timer will call this function, and the rotation will begin

function rotate() {
  jQuery('#nextabc').click();
}
#carouselabc {
  position: relative;
  width: 60%;
  margin: 0 auto;
}

#slidesabc {
  overflow: hidden;
  position: relative;
  width: 100%;
  height: 250px;
}

#areadoslideabc {
  list-style: none;
  width: 100%;
  height: 250px;
  margin: 0;
  padding: 0;
  position: relative;
}

#slidesabcdef {
  width: 100%;
  height: 250px;
  float: left;
  text-align: center;
  position: relative;
  font-family: lato, sans-serif;
}


/* Styling for prev and next buttons */

.btn-barabc {
  max-width: 346px;
  margin: 0 auto;
  display: block;
  position: relative;
  top: 40px;
  width: 100%;
}

#buttonsabc {
  padding: 0 0 5px 0;
  float: right;
}

#buttonsabc a {
  text-align: center;
  display: block;
  font-size: 50px;
  float: left;
  outline: 0;
  margin: 0 60px;
  color: #b14943;
  text-decoration: none;
  display: block;
  padding: 9px;
  width: 35px;
}

a#prevabc:hover,
a#next:hover {
  color: #FFF;
  text-shadow: .5px 0px #b14943;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="carouselabc">
  <div class="btn-barabc">
    <div id="buttonsabc">
      <a id="prevabc" href="#">Previous</a>
      <a id="nextabc" href="#">Next</a>
    </div>
  </div>
  <div id="slidesabc">
    <div id="jobshome"></div>
  </div>
</div>

脚本来源:https://codepen.io/TyStelmach/pen/yygvNK

【问题讨论】:

  • 你有没有想过使用全局标志。您可以使用 if 语句来查看状态是否存在为真或假。我一直在使用它,它对我有用。
  • 我在每个函数中使用了console.log("text"); 来查看是否一切正常。只有第二个功能是。
  • 您是否尝试过使用 .then 而不是 .done。 makandracards.com/makandra/…
  • 是的,我做到了。还是不行。

标签: javascript jquery .when when-js donejs


【解决方案1】:

为什么不用load()函数的回调呢?

$("#jobshome").load("jobs/newest-jobs .js-toprow", () => {
   //do stuff after loading the html
}); 

when 不起作用,因为它需要 PromiseDeferred 对象,如果两者都不是,它会将传递的值视为已解析的 Deferred(这基本上意味着它不会等待,而是执行 @ 987654328@/done马上)。

【讨论】:

  • =&gt;是什么意思?
  • 箭头函数,你不妨把() =&gt; 换成function(),更多:stackoverflow.com/questions/24900875/…
  • 我使用了你的解决方案,它奏效了。但后来我再也看不到幻灯片循环了。
  • 不知道你说的“幻灯片循环”是什么意思,但是你把内容加载后需要运行的所有代码都放到回调函数中了吗?
  • Uncaught DOMException: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent.
猜你喜欢
  • 1970-01-01
  • 2020-10-30
  • 1970-01-01
  • 2021-08-21
  • 1970-01-01
  • 2019-02-16
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多