When the solution matters

Tips...

Registration Documentation Knowledgebase Seminars / Training Partner Programs 4D Partner Central

4DLOOP

The next step in this tutorial will be to use 4DLOOP with a 4D method.

Create a new 4D Project Method and call it WSC_LoopTimeInit. Enter the following into the project method:

C_TEXT($0;$1)
C_LONGINT(WSC_i_MSEnd)

If (Count parameters>0)
$iSeconds:=Num($1)
Else
$iSeconds:=2
End if

WSC_i_MSEnd:=Milliseconds+(1000*$iSeconds)

screenshot

Your 4D project method should look like the one in the picture above.

Go to the Method menu and choose Method Properties, then select Available through 4DACTION option in the properties dialog. Press the OK button.

Create another 4D Project Method and call it WSC_LoopTimeBased. Enter the following into it:

C_LONGINT($1)
C_BOOLEAN($0)

WSC_i_LoopCount:=$1

$0:=(Milliseconds<=WSC_i_MSEnd)

screenshot

Your WSC_LoopTimeBased method should look like the one in the picture above. Go to the Method menu and choose Method Properties, then select Available through 4DACTION option in the properties dialog. Press the OK button.

Save and close all project methods. Return again to your text editor and open the index.shtml file. Delete the current contents of the file and replace it with the following:

<html>
<head>
<title>4D Web Tags</title>
</head>
<body>
<!--4DSCRIPT/WSC_LoopTimeInit/3-->
<h1>Loop Test</h1>
<p>
<!--4DLOOP WSC_LoopTimeBased-->
Milliseconds: <!--4DVAR Milliseconds--><br>
<!--4DENDLOOP-->
</p>
<p>Total # of times looped:<!--4DVAR WSC_i_LoopCount--></p>
<!--4DIF (compiled application=False)-->
<p><b>Try compiling the application and re-loading this page. You should get more loops.</b></p>
<!--4DENDIF-->
</body>
</html>

Save the index.shtml file and return again to your web browser. Type "127.0.0.1" into the address field to re-load the page. There should be a delay of a little more than three seconds before you see the result in the web browser.

screenshot

So what happened? The first thing we did was set the number of seconds to loop for. We did this in the index.shtml file, when we wrote: <!--4DSCRIPT/WSC_LoopTimeInit/3-->. We passed a "3" to the WSC_LoopTimeInit method. Next, we used 4DLOOP with the WSC_LoopTimeBased method. This method returned True until the number of seconds specified had been exceeded.

In the above example, WSC_LoopTimeBased was executed 4,461 times. We know this because in the WSC_LoopTimeBased method the 4D variable WSC_i_LoopCount is set to the value of the loop number. When you use a 4D Method with 4DLOOP, the first parameter is typed as a Long Integer, and it receives the number of times the loop is entered.

If you have 4D Compiler, try compiling this application and re-opening it in Compiled mode. You should see a healthy performance increase. Although, even un-compiled, performance is very good, yielding nearly 1,500 loops per second in this example.

 Previous Step  1 2 3 4   Next Step


International | Company | Contact 4D | Site Map | Privacy Policy | © 4D, Inc. 1995-2008 | Change font size: [A] [A] [A] | Print this page | 4D RSS Feeds