Jump to content

Calling multiple functions


otester

Recommended Posts

Seems that only one works at a time, each one when called (button press on page) individually (blank out all other functions) works and if I leave them unblanked only the last function works..

 

How can I get them all to display their data instead of just one of them?

 

 

Each function is like this, draws data from a MySQL database:

 

function loadFuntion_x() {
			if (window.XMLHttpRequest)
  				{// code for IE7+, Firefox, Chrome, Opera, Safari
  				xmlhttp=new XMLHttpRequest();
  				}
			else
  				{// code for IE6, IE5
  				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  				}
			xmlhttp.onreadystatechange=function()
  				{
  				if (xmlhttp.readyState==4 && xmlhttp.status==200)
    			        {
    			        document.getElementById("function_x").innerHTML=xmlhttp.responseText;
    			        }
  				}
			xmlhttp.open("GET","/scripts/x.php",true);
			xmlhttp.send();
}

 

jQuery code that is meant to call all functions:

 

$(document).ready(function(){
$('#function_all').click(function() {
	loadFunction_1();
	loadFunction_2();
	loadFunction_3();
	loadFunction_4();
	loadFunction_5();
	loadFunction_6();
	loadFunction_7();
	loadFunction_8();								
});
});

 

 

Any help would be greatly appreciated,

 

Thanks,

 

otester

Link to comment
Share on other sites

there is no proper way of doing this, HOWEVER, you can hack it together with timeouts :), timeouts will not halt other script execution (unless there is an alert)

 

      setTimeout('loadFunction_1()',5);
      setTimeout('loadFunction_2()',5);
      setTimeout('loadFunction_3()',5);
      setTimeout('loadFunction_4()',5);

Link to comment
Share on other sites

there is no proper way of doing this, HOWEVER, you can hack it together with timeouts :), timeouts will not halt other script execution (unless there is an alert)

 

      setTimeout('loadFunction_1()',5);
      setTimeout('loadFunction_2()',5);
      setTimeout('loadFunction_3()',5);
      setTimeout('loadFunction_4()',5);

 

Thanks for post but got a response off another forum this morning, turns out you need to do this:

 

...
if (window.XMLHttpRequest)
  				{// code for IE7+, Firefox, Chrome, Opera, Safari
  				[color=red]var[/color] xmlhttp=new XMLHttpRequest();
  				}
			else
  				{// code for IE6, IE5
  				[color=red]var[/color] xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  				}
...

Link to comment
Share on other sites

there is no proper way of doing this, HOWEVER, you can hack it together with timeouts :), timeouts will not halt other script execution (unless there is an alert)

 

      setTimeout('loadFunction_1()',5);
      setTimeout('loadFunction_2()',5);
      setTimeout('loadFunction_3()',5);
      setTimeout('loadFunction_4()',5);

 

Thanks for post but got a response off another forum this morning, turns out you need to do this:

 

...
if (window.XMLHttpRequest)
  				{// code for IE7+, Firefox, Chrome, Opera, Safari
  				[color=red]var[/color] xmlhttp=new XMLHttpRequest();
  				}
			else
  				{// code for IE6, IE5
  				[color=red]var[/color] xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  				}
...

 

ohhh.. I thought you meant executing functions at the same time not 1 after the other..

 

you know you can also do this...

 

function loadFuntion() {
            var xmlhttp = '';
            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            return xmlhttp;
}
function getAndPopulate(xmlhttp,num,pageToGet) {
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                     {
                     document.getElementById("function_"+num).innerHTML=xmlhttp.responseText;
                     }
              }
           xmlhttp.open("GET",pageToGet,true);
           xmlhttp.send();
}
getAndPopulate(loadFunction(),1,'/scripts/1.php');
getAndPopulate(loadFunction(),2,'/scripts/2.php');
getAndPopulate(loadFunction(),3,'/scripts/3.php');

 

with alot less code :)

Link to comment
Share on other sites

ohhh.. I thought you meant executing functions at the same time not 1 after the other..

 

you know you can also do this...

 

function loadFuntion() {
            var xmlhttp = '';
            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            return xmlhttp;
}
function getAndPopulate(xmlhttp,num,pageToGet) {
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                     {
                     document.getElementById("function_"+num).innerHTML=xmlhttp.responseText;
                     }
              }
           xmlhttp.open("GET",pageToGet,true);
           xmlhttp.send();
}
getAndPopulate(loadFunction(),1,'/scripts/1.php');
getAndPopulate(loadFunction(),2,'/scripts/2.php');
getAndPopulate(loadFunction(),3,'/scripts/3.php');

 

with alot less code :)

 

Damn this does save a lot of code, but I have other buttons which need to call scripts individually.

 

How do you call them properly?

 

I tried:

 

$(document).ready(function(){
$('#function_x').click(function() {
	getAndPopulate(loadFunction(),1,'/scripts/function_x.php');
});
});

 

But it didn't work, replacing the code with an alert message works so I know every apart from this:

 

getAndPopulate(loadFunction(),1,'/scripts/function_x.php');

 

 

EDIT: Hmm seems button might actually be broken I'll re-edit soon.

REDIT: Nope button does work just got mixed with another one  ::)

Link to comment
Share on other sites

Why are so many people misusing jQuery these days? Are you aware that jQuery provides Ajax functionality that will be allot more reliable than what you have written.

 

Didn't know, I'm new to the whole jQuery scene, only found out it existed when I had a mootools conflict so I had to build my own slideshow.

 

Any help to the last script or anything else would be great!

Link to comment
Share on other sites

Turns out you got to put the functions outside of the document.ready bit:

 

function loadFunction() {
            var xmlhttp = '';
            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            return xmlhttp;
}
function getAndPopulate(xmlhttp,num,pageToGet) {
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                     {
                     document.getElementById("function_"+num).innerHTML=xmlhttp.responseText;
                     }
              }
           xmlhttp.open("GET",pageToGet,true);
           xmlhttp.send();
}

$(document).ready(function(){
$('#function_button').click(function() {
getAndPopulate(loadFunction(),1,'/scripts/1.php');
getAndPopulate(loadFunction(),2,'/scripts/2.php');
getAndPopulate(loadFunction(),3,'/scripts/3.php');
});
});

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.