在函数javascript中运行函数后的代码

时间:2016-12-11 19:13:19

标签: javascript function return

我有这个功能:

function start_all(id) {

document.getElementById('status').innerHTML = "Starting...";

start(id); // This print too a status on the "status" div like: ID started

document.getElementById('status').innerHTML = "Done...";

}

我的问题是最后一行在函数start(id)之前运行;我需要按顺序运行。

我的结果是:

Starting...
Done...
ID started

我需要这样:

Starting...
ID started
Done...

2 个答案:

答案 0 :(得分:0)

function start_all(id) {

document.getElementById('status').innerHTML = "Starting...";

start(id,function(){
document.getElementById('status').innerHTML = "Done...";

});

}

这称为回调

答案 1 :(得分:-1)

这个怎么样:



   document.getElementById('status').innerHTML = "Starting...";
var status_id = start(id) // a string that returns the text 'ID Started'
document.getElementById('status').innerHTML = status_id;
document.getElementById('status').innerHTML = "Done...";