tags:

views:

30

answers:

2

I'm now developing one JS UDF which seems following coding.

<script>
<!-- 
 function alertmsg()
 {
  alert("Hello World");
 }
 for(p=1; p <= 2; p++)
 {
  alertmsg();
 }
 -->
</script>

Normally, Alert Msg will be came out two times because of loop count is 2. What I want is Alert Msg will be came out only one time even loop count is 3. Any idea will be appreciated in advance.

+1  A: 

You only want to execute the piece of code once in the loop? Do something like this:

var executed = false;
for(var i = 1; i <= 2; i++)
{
   if (!executed) {
      alertmsg();
      executed = true;
   }
}
reko_t
Wow..! great, dude.. thanks.
ppshein
+1  A: 

Or perhaps this?

  <script type="text/javascript">

        function alertmsg() {
            alert("Hello World");
        }

        for (var p = 0; p < 3; p++) {

            if (p == 2) {
                alertmsg();
            }

        }

    </script>
Alex Key