简体   繁体   English

首先完成后执行jQuery函数

[英]Execute jQuery function after first is complete

I have created a page where I have multiple functionality done but stuck with timing of it, if you see the page example here in JSFiddle . 如果您在JSFiddle中看到页面示例,那么我已经创建了一个页面,其中我完成了多项功能,但仍受其时间限制

When you open the page, it will first run a loader and when the loader is complete there are multiple boxes, which is showing one by one but in currently its not happening, Loader is loading fine and then in the next step where centered columns has to appear one by one, the first four columns loads by default and then other div loads one by one. 当您打开页面时,它将首先运行一个加载器,当加载器完成时,会有多个框,一个一个地显示,但当前没有发生,加载器正在加载,然后在下一步中居中的列要一一出现,默认情况下前四列会加载,然后其他div会一一加载。

My question is, how can I execute a function and execute another another function once the previous is complete. 我的问题是,一旦前一个功能完成,我该如何执行一个功能并执行另一个功能。

For loader I have the following: 对于装载机,我有以下内容:

  //--------- process bar animation
  var showText = function (target, message, index, interval) {   
    if (index < message.length) {
      $(target).append(message[index++]);
      setTimeout(function () { showText(target, message, index, interval); }, interval);
    }
  }

  var width = 100,
      perfData = window.performance.timing, // The PerformanceTiming interface represents timing-related performance information for the given page.
      EstimatedTime = -(perfData.loadEventEnd - perfData.navigationStart),
      time = parseInt((EstimatedTime/1000)%60)*100;

      showText("#msg", "Welcome to the Company Group", 0, width); 

  // Loadbar Animation
  $(".loadbar").animate({
    width: width + "%"
  }, time);

  // Loadbar Glow Animation
  $(".glow").animate({
    width: width + "%"
  }, time);

  // Percentage Increment Animation
  var PercentageID = $("#precent"),
          start = 0,
          end = 100,
          durataion = time;
          animateValue(PercentageID, start, end, durataion);

  function animateValue(id, start, end, duration) {

      var range = end - start,
        current = start,
        increment = end > start? 1 : -1,
        stepTime = Math.abs(Math.floor(duration / range)),
        obj = $(id);

      var timer = setInterval(function() {
          current += increment;
          $(obj).text(current + "%");
        //obj.innerHTML = current;
          if (current == end) {
              clearInterval(timer);
          }
      }, stepTime);
  }

  // Fading Out Loadbar on Finised
  setTimeout(function(){
    $('.preloader-wrap').fadeOut(300);
    $('.loader-wrap').fadeOut(300);
    $('.main-wrapper').fadeIn(300);
    $('body').addClass('bg');
  }, time);

For showing div one by one in next step I have the following code: 为了在下一步一步一步显示div,我有以下代码:

$(".column-wrapper").each(function(index) {
var $this = $(this);
setTimeout(function () { $this.addClass("show"); }, index * 1000);
});

I use trigger and on for those kind of things. 我使用triggeron了这种事情。 You had a lot of code, so sorry, I didn't want to read all of that but this is a simplified example. 您有很多代码,很抱歉,我不想阅读所有内容,但这是一个简化的示例。

https://jsfiddle.net/2d6p4L6k/1/ https://jsfiddle.net/2d6p4L6k/1/

 $(document).ready(function(){ var action = function(){ $('div.one').css('background-color', 'green'); /* do whatever you want to do */ /* then trigger(call) another function */ $(document).trigger('hello-action'); }; var hello = function() { $('div.two').css('background-color', 'fuchsia'); }; /* just listen if anything triggers/calls "hello-action" */ /* if so call function named hello */ $(document).on('hello-action', hello); setTimeout(action, 1500); }); 
 div { width: 50px; height: 50px; background-color: orange; margin: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="one">one</div> <div class="two">two</div> 

Note: To keep it simple we trigger and listen on $(document) which is really just to keep it simple. 注意:为简单起见,我们触发并监听$(document) ,这实际上只是为了使其简单。 A better approach would be to have a wrapper element, and then you can do exactly the same (trigger and listen) just on that element and not on the entire document. 更好的方法是拥有一个wrapper元素,然后可以仅对该元素而不是整个文档执行完全相同的操作(触发和监听)。 You can find more details on the jQuery API .trigger() 您可以在jQuery API .trigger()上找到更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM