功能未按顺序执行,如何 运行 前一个完成后

functions not executing in order , how to run one after previous has finished

我已经尝试了几件事,但无法按顺序发出警报

感谢任何帮助,你会推荐什么

function first(callback){
  setTimeout("alert('alert first')", 900);
  callback();
}

function second(callback){
  setTimeout("alert('alert 2nd')", 100);
  callback();
}

function thrid(callback){
  setTimeout("alert('alert 3rd')", 100);
  callback();
}

first(function() {
  second(function() {
    thrid(function() {
      alert('all 3 functions alerted in order alert this last');
    });
  });
});

您需要在超时函数中调用回调。否则他们不会等待时间延迟,他们会立即执行。

function first(callback) {
  setTimeout(function() {
    alert('alert first');
    callback();
  }, 900);
}

function second(callback) {
  setTimeout(function() {
    alert('alert 2nd');
    callback();
  }, 100);
}

function third(callback) {
  setTimeout(function() {
    alert('alert 3rd');
    callback();
  }, 100);
}

first(function() {
  second(function() {
    third(function() {
      alert('all 3 functions alerted in order alert this last');
    });
  });
});