Daniela: Endlosschleife in Script

Lässt sich dieses Script auch als Endlosschleife
verändern ?
Wie würde das Script dann aussehen ?

<head>
<script language="JavaScript">
  <!--

// Get browser type
  // Object "document.layers" only exists in Netscape Navigator
  if(document.layers)
  {
       ns = 1; ie = 0;
  }
  else
  {
       ns = 0; ie = 1;
  }

// Style Sheet for Netscape Communicator, tree items are positioned absolute.
  // Unlike Internet Explorer, attribute "visibility" is used to show
  // or hide tree items.
  if(ns)
  {
   document.write( "<style type='text/css'>" +
        ".child { position:absolute; visibility:hidden; }" +
        ".parent { position:absolute; visibility:show; }" +
       "</style>");
  }

// Style Sheet for Internet Explorer, tree items are positioned relative.
  // Unlike Netscape Communicator, attribute "display" is used to show
  // or hide tree items.
  if(ie)
  {
   document.write( "<style type='text/css'>" +
        ".child { display:none; position:relative; }" +
        ".parent { display:block; position:relative; }" +
       "</style>");
  }

//============================================================================
  // function NetscapeInit()
  //
  // Due to the relative positions of the tree items in Netscape Navigator,
  // each position has to be set before the first display.
  //============================================================================
  function NetscapeInit()
  {
   for(var iI=0 ; iI < document.layers.length; iI++)
   {
    document.layers[iI+1].top = document.layers[iI].y
    if (document.layers[iI].visibility == "show")
    {
     document.layers[iI+1].top += document.layers[iI].clip.height;
    }
   }
  }

//============================================================================
  // function ExpandShrink()
  //
  // Shows or hides an item in the tree identified by iItem
  //
  // Numeration is as follows:
  //
  // parent(0)
  //   |
  //   +-- child(1)
  // parent(2)
  //   |
  //   +-- child(3)
  // ...
  //============================================================================
  function ExpandShrink(iItem)
  {
   if(ns)
   {
    if(document.layers[iItem].visibility == "show")
    {
     for(var iI=iItem+1; iI<document.layers.length; iI++)
     {
      document.layers[iI].top -= document.layers[iItem].clip.height;
     }
     document.layers[iItem].visibility = "hide";
    }
    else if(document.layers[iItem].visibility == "hide")
    {
     for(var iI=iItem+1; iI<document.layers.length; iI++)
     {
      document.layers[iI].top += document.layers[iItem].clip.height;
     }
     document.layers[iItem].visibility = "show";
    }
   }
   if(ie)
   {
    if(document.all["item"+(iItem+1)].style.display == "none" || document.all["item"+(iItem+1)].style.display == "")
    {
     document.all["item"+(iItem+1)].style.display = "block";
    }
    else if(document.all["item"+(iItem+1)].style.display == "block")
    {
     document.all["item"+(iItem+1)].style.display = "none";
    }
   }
  }
if(navigator.userAgent.indexOf("Mac")>-1) MAC=true; else MAC=false;

document.onmouseover = light
document.onmouseout = dark

pulseInterval = null
pulseElement = null

pulsePosition = 0
pulseColor = new Array(
 "#ffcc00",
 "#e5b200",
 "#cc9900",
 "#b27f00",
 "#996600",
 "#7f4c00",
 "#663300",
 "#4d1a00",
 "#330000",
 "#190019",
 "#000033",
 "#190019",
 "#330000",
 "#4d1a00",
 "#663300",
 "#7f4c00",
 "#996600",
 "#b27f00",
 "#cc9900",
 "#e5b200"
 )
pulseColorText = new Array(
 "#663300",
 "#4d1a00",
 "#330000",
 "#190019",
 "#000033",
 "#190019",
 "#330000",
 "#4d1a00",
 "#663300",
 "#7f4c00",
 "#996600",
 "#b27f00",
 "#cc9900",
 "#e5b200",
 "#ffcc00",
 "#e5b200",
 "#cc9900",
 "#b27f00",
 "#996600",
 "#7f4c00"
 )

function light(e) {
 if(event.srcElement.tagName=="A") {
  pulseElement = event.srcElement
  pulseElement.style.color = "#000000"
  pulseInterval = setInterval("pulse()",50)
 }
}
function dark(e) {
 if(event.srcElement.tagName=="A") {
  clearInterval(pulseInterval)
  if(MAC==true) {
   pulseElement.style.backgroundColor = "#000033"
  } else {
   pulseElement.style.removeAttribute("backgroundColor")
  }
  pulseElement.style.color = "#ffcc00"
  pulsePosition = 0
 }
}
function pulse() {
 pulseElement.style.backgroundColor = pulseColor[pulsePosition]
 pulseElement.style.color = pulseColorText[pulsePosition]
 pulsePosition++
 if(pulsePosition==pulseColor.length) pulsePosition = 0
}
  //-->
  </script>
<title></title>

</head>

<body link="#000000">

<p><a href="http://">test</a></p>

  1. Hallo Daniela,

    ich hoffe, du nimmst es mir nicht übel, dass ich dein Script nicht mal richtig durchgesehen habe - es war ein bisschen viel auf einmal.

    Aber zunächst etwas grundsätzliches: Javascripts sollten nie in einer Endlosschleife laufen.
    Zum einen überwacht der Browser nämlich die Ausführungszeit von Skripten (zumindest der IE tut das) und gibt eine Warnmeldung aus, wenn ein Script länger als ein paar Sekunden am Stück läuft.
    Davon abgesehen läuft im Browser gar nix, solange ein Script ausgeführt wird. Kein Link, kein "ONMPUSEOVER", etc. Das ist viel schlimmer. Du legst also den ganzen Browser lahm, wenn du eine Endlosschleife produzierst.

    Besser ist es, den Programmablauf in Einzelschritte zu zerlegen, dann eine Scriptfunktion per setTimeout() oder setInterval() regelmäßig aufzurufen, und diese Funktion erledigt bei jedem Aufruf genau einen Programmschritt und kommt dann via return wieder zurück. So hast du sogar in der Hand, wie schnell dein "Programm" abläuft.

    Viel Spaß beim Probieren,

    Martin