Jump to content

Stupid Function Question


harshpandya

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...