Results 1 to 9 of 9

Thread: Javascript have an order?

  1. #1
    Join Date
    Feb 2009

    Unhappy Javascript have an order?

    Hi. I am doing some major hair pulling over something.
    I have this div in every page of our site, hidden by default, that is a transparent layer of the whole page with a loading gif in the middle. I use it for forms since we have so many people hit submit over and over, so, onsubmit, I show the div, let the page do it's thing, then hide it again (or another page is presented).
    Works great on form pages, where they submit to themselves or another page.
    However, I have another page that's sort of an application (multiple tabs/divs), so it saves each field via ajax.
    Basically, you click on one of the tabs on the page to be presented with that information. There is an "edit" button that when clicked hides all static fields and shows their relevant input fields. There is a save that then switches all these back one at a time, saving the edited field value as it goes. This function, like my others, shows/hides the div at beginning and end:
    function saveTabFields(tname){
    	var myDiv = document.getElementById(tname);
    	var inputArr = myDiv.getElementsByTagName("input");
    	var selectArr = myDiv.getElementsByTagName("select");
    	for(i=0; i<inputArr.length; i++){
    		if(inputArr[i].type!='radio' && inputArr[i].type!='checkbox' && inputArr[i].type!='hidden'){
    			if(document.getElementById('l2on').value=='0' && inputArr[i].name.substring(0,3)!="l2-")
    			else if(document.getElementById('l2on').value=='1')
    			if(in_array(inputArr[i].name,liquidAssets) || in_array(inputArr[i].name,nonLiquidAssets)){
    		if (navigator.appVersion.indexOf("Linux")==-1 && navigator.appVersion.indexOf("X11")==-1)
    	for(i=0; i<selectArr.length; i++){
    		if (navigator.appVersion.indexOf("Linux")==-1 && navigator.appVersion.indexOf("X11")==-1)
    		if(document.getElementById('l2on').value=='0' && selectArr[i].name.substring(0,3)!="l2-")
    		else if(document.getElementById('l2on').value=='1')
    	if(tname == 't6' || tname == 't9'){
    So basically, on save, show the 'loader' div, save each input via ajax, then hide the 'loader' div.
    Here's the problem, no matter how I do this (I've tried adding some sleep code between showing the loader and the rest, calling only he show loader line in this function and putting the rest in a function I then call using a setTimout, etc) it seems that everything in this function is called BEFORE the first line! The div always comes up, but it just flashes up after all the fields are saved and swapped back to static, as if the first line to show the div was just before the last line to hide it.
    Any suggestions? I think I've tried everything!

  2. #2
    Join Date
    Feb 2003
    Michigan, USA
    It's hard to tell with the code you've provided. Is the saveEdit() function initiating an AJAX call? Unless you specify it, AJAX calls are asynchronous, meaning your scripts continue to execute as the AJAX call is made. Can we see the code for the other functions used in the saveTabFields() function?

  3. #3
    Join Date
    Feb 2009

    No problem

    Sure, here is the rest, the ones I did not put in are long but simply take a couple fields and calculate another. To me it would seem even if AJAX is asynchronous that it just means when ajax is done the REMAINING code keeps executing, but since I show the "loader" div before any ajax is called, should that not still happen first? Thanks again:

    var httpObject = getHTTPObject();
    // Get the HTTP Object
    function getHTTPObject(){
    	if (window.ActiveXObject) 
    		return new ActiveXObject("Microsoft.XMLHTTP");
    	else if (window.XMLHttpRequest) 
    		return new XMLHttpRequest();
    	else {
    		alert("Your browser does not support AJAX.");
    		return null;
    //Function to swap input field and static field visibility after ajax saves field
    function updateCLValues(field,element){
    	document.getElementById(element+"s").innerHTML = document.getElementById(field).value;
    	if(document.getElementById(field).type!='radio' && document.getElementById(field).type!='checkbox' && document.getElementById(field).type!='hidden'){
    		if(document.getElementById(element+"e").style.display == "none"){
    			document.getElementById(element+"s").style.display = "none";
    			document.getElementById(element+"e").style.display = "inline";
    		} else {
    			document.getElementById(element+"e").style.display = "none";
    			document.getElementById(element+"s").style.display = "inline";
    //Ajax function to save new field value
    function saveEdit(field,element,module,rid,call){
    	if (httpObject != null) {
    		//alert ("/crmPortal/clVEProcessor.php?module="+module+"&id="+rid+"&field="+field+"&value="+document.getElementById(field).value);
    		httpObject.open("GET", "/crmPortal/clVEProcessor.php?module="+module+"&id="+rid+"&field="+field+"&value="+document.getElementById(field).value+"&function="+call+"&oppid=<?=$loan->id?>&primid=<?=$borrower->id?>", true);
    		httpObject.onreadystatechange = updateCLValues(field,element);

  4. #4
    Join Date
    Feb 2003
    Michigan, USA
    Ah, ok. When the saveTabFields() is called, it should show the "loading..." DIV. Now the trick is to hide the loading DIV after all the AJAX calls have completed. You can't start all the AJAX calls at once. You've got to do them one at a time. The saveTabFields() function starts one AJAX call. When that one finishes, the onreadystatechange function calls the second AJAX call. When that finishes, the onreadystatechange function calls the third ... and so forth until all the fields have been saved.

  5. #5
    Join Date
    Feb 2009


    Thanks, but before you posted that I tried simplifying it by taking everything out and simply doing the following:
    function saveTabFields(tname){
    	//The pausecomp function directly inserted for simplicity and testing
    	var date = new Date();
    	var curDate = null;
    	do { curDate = new Date(); }
    	while(curDate-date < 2000);
    	//END inserting pausecomp
    This makes it even stranger. This should effectively show the loader div for 2 seconds then hide it again, right? No such luck, the same thing happens, only now it happens so fast you don't even see the loader div flash, just nothing. But if I comment all this too and just make the loader div show, that works! Am I just missing something totally simple?

  6. #6
    Join Date
    Feb 2009


    At least solved for my needs.
    Apparently it did have something to do with what you mentioned, it's just that the pausecomp function I was using is apparently not working, so no matter what it was going directly from the show to hide lines since the ajax in between was asynchronous as you said.
    I had tried using setTimeout before in many other places, but I just tried it in another place I had not before now and I can get the effect I want, I used a setTimeout on the final line that re-hides the div!
    Thanks for all your help, any idea why the pausecomp code was not doing what I wanted it to do?

  7. #7
    Join Date
    Jan 2009
    Why don't you just call a function to hide the div after the last ajax call is finished?

  8. #8
    Join Date
    Feb 2003
    Michigan, USA
    Quote Originally Posted by criterion9 View Post
    Why don't you just call a function to hide the div after the last ajax call is finished?
    Because AJAX calls do not finish in the same order they were instantiated if they are asynchronous.

  9. #9
    Join Date
    Jan 2009
    You would know how many calls you have made and thus when the last open connection is closed so it would work.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.15745 seconds
  • Memory Usage 2,953KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (9)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates