0

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.

2
  • Regardless of what your server-side language is, this is a JavaScript function. In CF, the term "UDF" is typically reserved for ColdFusion functions, which are server-side only. Commented Aug 18, 2010 at 13:07
  • I agree Ben Doom, in javascript it is referred to as a function. But remember that t-sql also uses the term UDF. Commented Aug 18, 2010 at 15:22

2 Answers 2

1

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;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Or perhaps this?

  <script type="text/javascript">

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

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

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

        }

    </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.