Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22771

Re: Wait between different Calls (procedures)

Message-ID <10872400.WxMVP94fBA@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2014-01-24 02:54 +0100
Subject Re: Wait between different Calls (procedures)
Newsgroups comp.lang.javascript
References <d583e3d7-af49-42a3-a979-a48d294537d0@googlegroups.com>

Show all headers | View raw


frank.catry@gmail.com wrote:
^^^^^^^^^^^^^^^^^^^^^
Please add your real name.

> i have to populate 3 <select></selects> dom's regarding the content of the
> database. Wehn the user wish to update a record. In my case Class, Make
> and model of a car. However when i try to execute the 3 getContent calls
> on the bottom of the code it doesn't work.

“Doesn't work” is not a problem description.  

Please read <http://PointedEars.de/faq#posting> pp.

> One by one is oké. How can i
> put a WAIT till finished between the getContent calls ?

You cannot.  However, what you really appear to be asking for is how you can 
call a function only after the response requested through a previous 
function call has been received.  In that case, it is rather simple:
Make the next function call *only then*.

Your code, although already error-prone, and hard to read due to lack of a 
consistent code style, could be adapted only slightly as follows:
 
> <?

This should always be

  <?php

Also, if possible, set short_open_tag = Off in php.ini, and use PHP 5.4+ 
instead where you do not need it for <?= … ?> anymore.  If you are still 
using PHP < 5.3, you should upgrade ASAP for security and other reasons.

> //data from the database;
> $oriClasse="Car";
> $oriMake="BMW";
> $oriModel="118";
> ?>
> 
> <script type="text/javascript">

  var req = {
    data: <?= json_encode(array(
      array(
        'id'  => 'theClasses',
        'uri' => "getClasses.php?key={$oriClasse}"
      ),
      array(
        'id'  => 'theMakes',
        'uri' => "getMakes.php?key={$oriClasse}&dbvalue={$oriMake}"
      ),
      array(
        'id'  => 'theModels',
        'uri' => "getModels.php?key={$oriMake}&dbvalue={$oriModel}"
      )
    )); ?>,

    index: -1,

    getNextItem: function () {
      var _data = this.data;
 
      if (this.index < _data.length - 1)
      {
        return _data[++this.index];
      }

      return null;
    }
  };

  var req_item = req.getNextItem();

All your other functions could/should become methods of this or another 
object.

Do not forget to escape your input.

> var domObj="";
> 
> function getContent(theDom,theUrl){
> 
> ajaxObj = GetXmlHttpObject();
> domObj=theDom;
> ajaxObj.open("GET", theUrl , true);
> ajaxObj.onreadystatechange=updateDom;
> ajaxObj.send(null);
> }
> 
> function updateDom(){
>     if (ajaxObj.readyState == 4 || ajaxObj.readyState == "complete"){

What is the DOM implementation where XHR::readyState is a *string* value?

>         document.getElementById(domObj).innerHTML=ajaxObj.responseText;

      if ((req_item = req.getNextItem()))
      {
        getContent(req_item.id, req_item.uri);
      }

>     }
> }
> 
> 
> function GetXmlHttpObject () {
> var xmlHttp = null;
>  try {
>    // Firefox, Opera 8.0+, Safari, IE 7+
>    xmlHttp = new XMLHttpRequest();
>  } catch (e) {
>    // Internet Explorer - old IE - prior to version 7
>    try {
>       xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
>    } catch (e) {
>       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
>    }
>  }
> return xmlHttp;
> }

  getContent(req_item.id, req_item.uri);

However, if you only create a new XHR object when a request still is in 
progress, you can increase speed and save memory.  In that case you can keep 
your calling code as it is but you probably have to write or use an XHR 
wrapper.  See also <http://PointedEars.de/wsvn/JSX/trunk/https.js> for one.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Wait between different Calls (procedures) frank.catry@gmail.com - 2014-01-23 00:20 -0800
  Re: Wait between different Calls (procedures) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-01-23 11:06 +0100
  Re: Wait between different Calls (procedures) Christoph Michael Becker <cmbecker69@arcor.de> - 2014-01-23 17:26 +0100
    Re: Wait between different Calls (procedures) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-24 02:05 +0100
      Re: Wait between different Calls (procedures) Christoph Michael Becker <cmbecker69@arcor.de> - 2014-01-24 02:44 +0100
        Re: Wait between different Calls (procedures) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-25 12:01 +0100
  Re: Wait between different Calls (procedures) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-24 02:54 +0100
    Re: Wait between different Calls (procedures) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-25 18:03 +0100

csiph-web