0

How to run sequence functions in JavaScript: I've 4 functions like this:

function myfunction()
{
   callfunction1();
   callfunction2();
}

In callfunction1:

function callfunction1()
{
    callfunction3();
}

My problem is in "myfunction" when I call "callfunction1",callfunction1 is run and it call "callfunction3" but when "callfunction3" haven't finish,"callfunction2" is running.Have anyway to wait "callfunction3" finish then "callfunction2" is run ?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user2265231
  • 79
  • 1
  • 9
  • It's called JavaScript Callback Functions. Read: http://www.impressivewebs.com/callback-functions-javascript/ – Raptor Jun 20 '13 at 02:53

1 Answers1

3

JavaScript is single-threaded, callfunction2 will only run after callfunction3 has compeletely finished.

However, if callfunction3 contains some AJAX or other asynchronous operation, then that will always happen after callfunction2 (because of the single-threaded thing, it can't start the asynchronous callback until after the synchronous stuff is done). Anything that relies on the result of an asynchronous function MUST either be in the function itself, or called by said function.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592