Velden updaten in loop

Status
Niet open voor verdere reacties.

LucGT

Nieuwe gebruiker
Lid geworden
15 nov 2007
Berichten
2
Hallo,

Ik probeer via een loop twee form fields te updaten in mijn form.
Het probleem is dat enkel de laatste waarden uit de 'doloop' functie uiteindelijk in de form fields terecht komen.
Is het zo dat de fields enkel kunnen geupdate worden bij het beëindigen van de functie of kan ik dit in de functie tussentijds al forceren ?
In de functie is een vertragingsfunctie ingebouwd zodat je de waarden elk één seconde zou moeten zien verschijnen.

Momenteel heb ik de code aangepast zodat ik driemaal manueel probeer een waarde in de velden te plaatsen maar dit lukt dus niet.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <title>test</title>
  <script type="text/javascript" language="javascript">



function doloop() {

		document.frm1.start.value = "Start1"
		document.getElementById("destination").value = "Destination1"
		
		pause(1000)
		
		document.frm1.start.value = "Start2"
		document.getElementById("destination").value = "Destination2"

		pause(1000)
		
		document.frm1.start.value = "Start3"
		document.getElementById("destination").value = "Destination3"

}

function pause(millis) 
{
        var date = new Date();
        var curDate = null;

        do { curDate = new Date(); } 
        while(curDate-date < millis)
}
  </script>
</head>

<body>
<FORM action = "" Name ="frm1">
    <br />
    
    <input 
   type="hidden" 
   name="redirect" 
   value="/default.html">

    
    <div style="width:700px;border-width:0px;border-style:solid">
      <input type="button" id="test" value="test" onclick="doloop()" />
      <br />
      <br />
      <div style="width:350px;border-width:0px;border-style:solid">
        <table id="selection" class="selection" cellpadding="1" cellspacing="1">
          <tr>
            <td width="276px">Start:</td>
            <td width="424px">
            <input type='text' id='start' style="width:250px;" value='start' size="20"/></td>
          </tr>
          <tr>
            <td width="276px">Destination:</td>
            <td width="424px">
            <input type='text' id='destination' style="width:250px;" value='destination' size="20"/></td>
          </tr>
        </table>
      </div>
      <br />

</form>
</body>
</html>
 
Laatst bewerkt:
Een vertraging is niet zo mooi omdat je dan eigenlijk de processor een seconde 'bezet' houdt. De waarden die de functie in het document wijzigd worden idd pas geupdate als de functie klaar is, dus alleen de laatste maal.

setTimeout() werkt beter:
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <title>test</title>


<script type="text/javascript">
 var nr;
 function startloop() {
  nr = 1;
  doloop();
  };

 function doloop() {
  document.frm1.start.value = 'Start' + nr;
  document.getElementById("destination").value = 'Destination' + nr;
  nr++
  if (nr < 4) setTimeout('doloop()',1000);
  };
</script>


</head>

<body>
<FORM action = "" Name ="frm1">
    <br />
    
    <input 
   type="hidden" 
   name="redirect" 
   value="/default.html">

    
    <div style="width:700px;border-width:0px;border-style:solid">
      <input type="button" id="test" value="test" onclick="startloop()" />
      <br />
      <br />
      <div style="width:350px;border-width:0px;border-style:solid">
        <table id="selection" class="selection" cellpadding="1" cellspacing="1">
          <tr>
            <td width="276px">Start:</td>
            <td width="424px">
            <input type='text' id='start' style="width:250px;" value='start' size="20"/></td>
          </tr>
          <tr>
            <td width="276px">Destination:</td>
            <td width="424px">
            <input type='text' id='destination' style="width:250px;" value='destination' size="20"/></td>
          </tr>
        </table>
      </div>
      <br />

</form>
</body>
</html>
Zo wordt de functie doloop() 3x achter elkaar aangeroepen met nr = 1, 2, en 3. :)


Vr.Gr. Egel.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan