PDA

View Full Version : Queueing the functions (JavaScript)



lauthiamkok
12-20-2008, 10:14 PM
Basically, I have a list of functions I want to queue when a button is executed. I have read some examples and threads online but I still cannot find the right direction. Please help if you have any idea.

For instance,

function list()
{
function1();
function2();
function3();
function4();
}

<input type="button" value="Button00" onclick="list()" />

Many thanks,
Lau

jscheuer1
12-21-2008, 01:28 AM
The functions will execute in the order that they appear in the function list() - However, depending upon whatever happens in these functions, control may get handed off to one of the others during a pause or perceived ending in the action in another.

Javascript is not a multi-threaded language, so whatever code is using the interpreter will continue to do so until it either pauses or finishes using the interpreter.

If a function you call in list() hands off to yet another function, depending upon how it does so, any given browser might interpret that as the end of that function and then start processing the next one in list().

Bottom line, javascript is kind of screwy where this sort of thing is concerned, and even that varies at times depending upon the browser. It's best to write code that is dependant upon the order of execution of things in a single function.

That said, if there is a particular sequence of events that you are trying to set in motion, as opposed to just a general idea of events as presented in your post, give us a link to the page where there is a problem, and we can probably help you sort it out.

lauthiamkok
12-21-2008, 01:55 AM
Hello John! many thanks for the reply. I have asking this issue around online...

This is the actual entire code. It is to load AJAX request in queue actually in a 'clever' way (I think)...

<script>
function getXMLHttpRequestObject()
{
// Initialize the object:
var req = false;
// Choose object type based upon what's supported:
if (window.XMLHttpRequest)
{
// IE 7, Mozilla, Safari, Firefox, Opera, most browsers:
req = new window.XMLHttpRequest();
}
else if (window.ActiveXObject)
{ // Older IE browsers
// Create type Msxml2.XMLHTTP, if possible:
req = new window.ActiveXObject("Msxml2.XMLHTTP");
}
else
{
req = new window.ActiveXObject("Microsoft.XMLHTTP");
}
return req;
}

function get(url, target)
{
var req = null;
req = getXMLHttpRequestObject();
if(req)
{//alert( startRow );
req.onreadystatechange = function()
{
if(req.readyState == 4)
{
var result = document.getElementById(target);
result.innerHTML = req.responseText;
}
else
{
var loading = document.getElementById(target);
loading.innerHTML = 'loading';
}
}
req.open('get', url, true);
req.send(null);
}
else
{
alert('Request failed.');
}
}

function getlist()
{
var onLoadActions = new Array();
function addOnLoadAction(action)
{
onLoadActions[onLoadActions] = action;
}

function performOnLoadActions()
{
for (var count =0; count < onLoadActions.length; count++)
{
eval(onLoadActions[count]);
}
}
addOnLoadAction(get('text1.php','content1'));
addOnLoadAction(get('text2.php','content2'));
addOnLoadAction(get('text1.php','content3'));
addOnLoadAction(get('text2.php','content4'));
addOnLoadAction(get('text1.php','content5'));
addOnLoadAction(get('text2.php','content6'));
addOnLoadAction(get('text1.php','content7'));
addOnLoadAction(get('text2.php','content8'));
addOnLoadAction(get('b_get.php','content9'));
}


</script>



<a href="#" onClick="getlist(); return false;">Load</a>
<div id="content1">
</div>

<div id="content2">
</div>

<div id="content3">
</div>

<div id="content4">
</div>

<div id="content5">
</div>

<div id="content6">
</div>

<div id="content7">
</div>

<div id="content8">
</div>

<div id="content9">
</div>

lauthiamkok
12-21-2008, 01:55 AM
This is the first function I did to load the list in a function but it doesnt load the contents in queue,

function list()
{
get('text1.php','content1');
get('text2.php','content2');
get('text2.php','content3');
get('text1.php','content4');
get('text2.php','content5');
get('text2.php','content6');
get('text1.php','content7');
get('text2.php','content8');
get('b_get.php','content9');
}

This is the latter code I did but I notice that this code doesnt load the list in queue at first click but it 'somehow' does in the next click - i dunno why! I describe it 'somehow' bcos it load the last item in the list first then load other items in queue! any idea why???

function getlist()
{
var onLoadActions = new Array();

function addOnLoadAction(action)
{
onLoadActions[onLoadActions] = action;
}

function performOnLoadActions()
{
for (var count =0; count < onLoadActions.length; count++)
{
eval(onLoadActions[count]);
}
}
addOnLoadAction(get('text1.php','content1'));
addOnLoadAction(get('text2.php','content2'));
addOnLoadAction(get('text1.php','content3'));
addOnLoadAction(get('text2.php','content4'));
addOnLoadAction(get('text1.php','content5'));
addOnLoadAction(get('text2.php','content6'));
addOnLoadAction(get('text1.php','content7'));
addOnLoadAction(get('text2.php','content8'));
addOnLoadAction(get('b_get.php','content9'));
}

lauthiamkok
12-21-2008, 02:01 AM
I found this site which it does the queue perfectly but it is too difficult to adapt it - I am not good in JavaScipt at all!

http://www.jimroos.com/2007/05/queuing-ajax-requests.html