简体   繁体   English

在setTimeout()回调完成后执行JS代码

[英]Execute JS code after setTimeout() callback completes

I have the following code: 我有以下代码:

function newGraphic() {
  $.post('./lexiconGraph.pl', {
    'tag' : this.getAttribute('apolo'),
    'apoloKey' : this.id,
    'h' : surface.h,
    'w' : surface.w,
    'operation' : 'newGraphic'
  }, function(result) {
    minGraphic();
    // STOP AND WAIT //
    document.getElementById('container').innerHTML = result;
    getNodes();
    // STOP AND WAIT //
    maxGraphic();
  });
}

function minGraphic() {
  if (z > 0.01) {
    zoom(parseFloat(z - 0.01));
    setTimeout('minGraphic();', 5);
  }
}

The problem is: The function minGraphic creates an "animation effect" of unknown duration ( depends on the size of the graph ). 问题是:函数minGraphic创建一个持续时间未知的“动画效果”( 取决于图形的大小 )。 I need to find a way to make the following lines are executed only when the function minGraphic finished . 我需要找到一种方法, 只有当函数minGraphic完成时才会执行以下行。
Does anyone have any idea how I can do this? 有谁知道我怎么能这样做?


NOTE: Put the following lines in the function minGraphic is not an option, since I already use this function elsewhere. 注意:在函数minGraphic中放置以下行不是一个选项,因为我已在其他地方使用此函数。

Use a callback for when the animation is complete. 动画完成时使用回调。 Something like this: 像这样的东西:

function newGraphic() {
  $.post('./lexiconGraph.pl', {
    'tag' : this.getAttribute('apolo'),
    'apoloKey' : this.id,
    'h' : surface.h,
    'w' : surface.w,
    'operation' : 'newGraphic'
  }, function(result) {
    var afterAnimation = function()
    {
      document.getElementById('container').innerHTML = result;
      getNodes();
      maxGraphic();
    };

    minGraphic(afterAnimation);
  });
}

function minGraphic(cb) {
  if (z > 0.01) {
    zoom(parseFloat(z - 0.01));
    setTimeout(function() { minGraphic(cb); }, 5);
  }
  else
  {
    if(cb) cb();
  }
}

If I'm following correctly, what this will do is trigger the start of miniGraphic, passing in a callback to a function that will execute after the animation is complete (ie z is not > 0.01). 如果我正确地关注,那么它将触发miniGraphic的开始,将回调传递给将在动画完成后执行的函数(即z不大于0.01)。 When that event happens, if a callback ( cb ) was passed in, it will call that function, thus triggering the next step in the animation. 当该事件发生时,如果传入回调( cb ),它将调用该函数,从而触发动画中的下一步。

If you have stuff you need to wait on for the maxGraphic method, then you can adopt the same concept. 如果你有东西需要等待 maxGraphic方法,那么你可以采用相同的概念。

Alternatively, if jQuery is available to you, jQuery has built-in ways for post-animation callbacks that you can use. 或者,如果jQuery可供您使用,jQuery具有可以使用的动画后回调的内置方法。 See this documentation 请参阅此文档

You could use a callback? 你可以使用回调吗?

function minGraphic(callback) {
  if (z > 0.01) {
    zoom(parseFloat(z - 0.01));
    setTimeout($.proxy(minGraphic, this, callback), 5);
    return;
  }
  callback && callback();
}

Then just call the function like: 然后只需调用函数:

minGraphic(function () {
    //complete
});

Note that the callback argument is optionnal to stay backward compatible with previous code not interested in knowing when the operation was completed. 请注意, callback参数是optionnal,以保持向后兼容以前不感兴趣知道操作何时完成的代码。

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

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