harshpandya Posted July 18, 2008 Share Posted July 18, 2008 I am having hardtime understanding the how functions works in javascriptso here it is ::::lets say i call three functions:::fun1();fun2();fun3();now will javascript finish fun1 first and then go to fun2 and so on... or just call each functions simultaneously...can i get some details how it works - it bothers me so much I am sorry but i am newbieThanks, Link to comment Share on other sites More sharing options...
boen_robot Posted July 18, 2008 Share Posted July 18, 2008 In general, it's syncronously, i.e. the first function will first be finished before the second begins.But there are some interal JavaScript APIs and functions that can execute asyncronously and if fun1() contains such a function, it may turn out that fun1() is considered finished before the asyncronous function is over.Example of such an API is the XMLHttpRequest() object, also known as AJAX (standing for Asyncronous JavaScript And XML). The HTTP request can be started at one function and end after that function is over.Other examples are setTimeout() and setInterval() functions. They execute asyncronously, meaning that a function that calls them will keep going on even before the timeout/interval has elapsed. Link to comment Share on other sites More sharing options...
harshpandya Posted July 18, 2008 Author Share Posted July 18, 2008 This is exactly my problem is......I want these function to run sycronously - Is there a way around of this or something because i will have use AJAX functions and setTimeout and all.Please help Thanks Link to comment Share on other sites More sharing options...
Synook Posted July 19, 2008 Share Posted July 19, 2008 You can put the next function call inside the onreadystatechange handler of the previous AJAX call if needed.E.g. function fun1() { var xmlHttp = new XMLHttpRequest(); xmlHttp.onreadystatechange = function() { if (this.readyState == 4) { doSomething(); fun3(); setTimeout("fun2()", 5000); } } //open, send}fun1(); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.