SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    stop script until other sciprt has finished

    I have a function, which calls a second function and then does something. I need the second function to have finished before the initial function continues - how can i do this?

    cheers

    monkey
    monkey - the rest is history

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, what you're saying is:

    Code JavaScript:
    function init() {
        doSomething(); // First function to execute after init is called
        doSomethingElse(); // Second function to execute after init is called
    }
     
    function doSomething() {
        /**
        *   I do something here
        *   It's all me now, nothing else is being executed, because I'm not
        *   running asynchronous!
        */
    }
     
    function doSomethingElse() {
        /**
        *   And I supposed to wait for doSomething to finish.
        *   It's all me now, nothing else is being executed, because I'm not
        *   running asynchronous!
        */
    }
     
    /**
    *   Suppose I have the addEvent function written by Scott Andrew
    *   When the page is loaded, the function init is executed automatically.
    */
    window.addEvent(window, "load", init, false);

    It's doing exactly what you ask here.
    JavaScript doesn't execute functions asynchronous, unless you say so.
    You can make functions run asynchronous, but you'll have to use an XMLHttpRequest for that: the callback function runs asynchronously.
    By using setTimeout or setInterval to execute a function, it looks like it's run asynchronously, but it isn't.
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have:

    Code:
    function doStuff(){
          goAndPopulateSelectMenu();
          some code to loop through select menu and set one to 'selected'
    }
    The trouble is that the 'set selected' bit doesn't work. However, if i put an alert("ufgyug") before this bit of code it works fine. I am presuming that the alert pauses the script long enough for the goAndPopulateSelectMenu(); function to finish?

    monkey
    monkey - the rest is history

  4. #4
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, I have gone away and set it ip as you have done, wqith 3 functions, the 1st running the second to, 1 after the other, and it still doesn't work, but still works with the alert.

    Does it make any difference that the goAndPopulateSelectMenu() function calles an AJAX script?

    monkey
    monkey - the rest is history

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by boxhead View Post
    Does it make any difference that the goAndPopulateSelectMenu() function calles an AJAX script?
    If, by "AJAX script" you mean an asynchronous XmlHttpRequest, then yes.

  6. #6
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're manipulating the DOM it is very important information

    -Are you using an onload event to execute your script?
    -How do you create a XMLHttpRequest (use the 3 different ways to do that)
    -Does the callback function execute properly?
    -...
    FOR SALE: 1 set of morals, never used, will sell cheap

  7. #7
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    this.createAJAX = function() {
    		try {
    			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e1) {
    			try {
    				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e2) {
    				this.xmlhttp = null;
    			}
    		}
    
    		if (! this.xmlhttp) {
    			if (typeof XMLHttpRequest != "undefined") {
    				this.xmlhttp = new XMLHttpRequest();
    			} else {
    				this.failed = true;
    			}
    		}
    	};
    I don't use onload, the script is activated by clicking links on the page.

    By callback, do you mean gettting the results back? If so then everything works fine. The AJAX script I have, was on the internet for populating a select box with location, and i've adapted it slightly.
    monkey - the rest is history

  8. #8
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Indeed, the callback function is the function that's called when the request is done.

    Ok, seems fine so far...
    Can you post your entire code, or even better, put an example online.
    FOR SALE: 1 set of morals, never used, will sell cheap

  9. #9
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, i'll explain my process (sorry can't post code due to security):

    I have a page which uses FusionCharts - a Flash charting system which uses xml. On the page, the user can choose the scope of the chart (by division, town or area). A radio button is used to select the scope, which then calls a page (AJAX - sorry if this isn't the correct term, but you undersatnd my meaning)) to retrieve the relevant locations for a select menu (eg all the towns). When the user makes their selection, Ajax is used again to collect the xml for the chart and also adds a breadcrumb of the locations currently viewed (divisionA > Mytown > Thislocation) to the page. The locations in the breadcrumbs are also links to be clicked on to reload the page for that location (eg divisionA).

    The Alax code i use was from here:

    http://www.twilightuniverse.com/projects/sack/

    I won't post the code for the locations as it works fine and is simlar in principle to the rest.

    When one of the breadcrumb loactions is clicked here is the code which runs:

    Code:
    function changeScope(scope, location){
    	getLocations(scope); (changes all the select locations to relevant scope eg towns)
    	changeRadio(scope);
    	changeSelected(location) ;
    	updateChart('myChart');//this runs a script which retrieves the scope and location from the radio button and select, send it to the page (AJAX!) as a url param and receives xml (and some javascript vars) back
    }
    function changeRadio(scope){
    	var scopeVal; 
    	var a = document.forms[0].elements["n_scopeVal"]; 
    	for (var i=0; i<a.length; i++) { 
        	if (a[i].value==scope) { 
    			a[i].checked=true;
    			break; 
    		}
    	}
    }
    function changeSelected(location){
    	//alert("test")
    	var b = document.forms[0].elements["locations"]; 
    	for (var j=0; j<b.length; j++) { 
        	if (b[j].text==location) { 
    			b[j].selected=true; 
    			break; 
        	}
    	}
    }
    The other bit of code that may be of interest, is the code which gets the xml. The page which builds the xml, response.writes the outcome and also a javascript innterHTML= statment to set the breadcrumb, the eval gets this bit running (i don't know if this is correct or if there are better ways of doing it?)

    Code:
    var dataAjax = new sack();
    
    function getXML(yr){	
    	urlStr="xmldata_ajax.asp"+(getParams())
    	dataAjax.requestFile = urlStr;		
    	dataAjax.onCompletion = makeChanges ; that will be executed after file has been found		
    	dataAjax.runAJAX();		// Execute AJAX function
    }
    
    function makeChanges(){
    	strXML=(dataAjax.response);
    	var chartObj = getChartFromId('myChart');
    	chartObj.setDataXML(strXML);
    	eval(strXML)
    }
    any help? I can post more if required

    cheers

    monkey
    monkey - the rest is history


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •