3
Timers and Variables
You obviously must be wondering
where we are headed. By the time you finished the last chapter, you should have
learned all that you need to know to move from one card to another. If you have
not yet done so, then you will need to go out and buy another copy of this
book.
You ask why? Is this not
the first page of the book? If it is not, you have not quite followed our
instructions so far. When we said you are to eat you way into WML, if you
wanted it to be a part of you, we meant it. How else will WML get into you?
Reading is the slow, painful method of learning. We have perfected this art of
'eat to your brain's content.' You may not have realised it, but the text that
is written herein, and the paper on which it is printed together form a deadly
combination to make you learn. We have not yet filed a patent, for if this
technology is in anyway leaked to the public, then we would indeed have too
many people become extra smart.
It would be a paradoxical
situation where those who do not, or cannot use their head to stop eating, finally
would become smart enough to stop eating because they would put on weight. Yet,
should they stop eating, they would stop being smart enough, therefore they
would have to eat enough to have the brains to stop eating, which would mean
that they are not smart enough to be dumb enough to start eating in the first
place.
If you were wondering what
that was all about, wonder no more. We just wanted to drive a point home. It is
always easy to get carried away with something all the time. And with mobile phones
one has to be a bit more careful. What if the mobile phone user suddenly gets
engrossed in something, and forgets that he just keyed in the access codes to
his bank. One would want therefore to clear the screen after a point of time.
This is where timers come in. And you will quickly see, that to make use of
timers, you will have to use variables as well.
At the end of the chapter
2, you learned how to move from one card to another card, whether from the same
deck or another. We also explained why the onevent with onenterforward didn't
execute the rest of the card. The most sensible thing would be having no code
in that card. There is no point having <p> </p> because it will
never ever be displayed.
|
Screen 3. 1 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' id="a1" >
<onevent type="onenterforward">
<go href="#a2">
</go>
</onevent>
<p>
Card 1
</p>
</card>
<card title='yes' id="a2" >
<p>
Card 2 $aa
</p>
</card>
</wml>
Here we are taken to card
a2. If you notice, earlier the <go> had no content, now there is content.
If it has content, then you have to start with a <go>, enter what you
want and then end with a </go>.
If it doesn't have content at that specific time, then you end it with a
/> i.e. <go />. Here we chose
to do it the new way. Why? We will soon explain.
Now in card a2, you will
realise that the $aa doesn't get printed. You should have suspected by now that
this has something to do with $. After all WML was developed in America, and
the Americans always keep saying 'There's no such thing like a free lunch.'
Let's understand the
example better by putting it in context. You would like to know the share price
of a particular script, like Infosys perhaps. On Nasdaq, all these stocks have
3 or four letter codes. So you key in INFY, and this travels across the
Internet to some web server that understands the code. This web server would
then send back the share price of Infosys at that moment. This value would have
to be stored somewhere. Maybe, you would like to sell or buy shares over the
phone. Today you'd like to buy 10 Microsoft shares, and tomorrow sell 20 IBM
shares. All this data is subject to change, or is variable. And this needs to
be stored somewhere, which is why you make use of variables.
You need an entity to store
changing information or what we call a variable. In wml, a variable stores values. To print out the current value
of the variable, you precede the variable with a $ sign. Hence, $aa is a
variable. And since it has not been
created nor has any value stored in it, nothing is displayed. Normally a variable is set or unset. For the
moment, the variable has no value, so it is unset.
Within the <go> and the </go> you can have a setvar. i.e. a setvar is used within a task like <go>. This is one more rule to be followed up like the others; i.e. text can go anywhere but within <p> </p>; a <do> cannot be placed within <p>....... In this example, we define aa using setvar - setvar name=aa. aa becomes the name of our variable and the value given is 1ab. You can give your own values here. It really does not matter.
|
Screen 3. 2 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' id="a1" >
<onevent type="onenterforward">
<go href="#a2">
<setvar name="aa" value="1ab"/>
</go>
</onevent>
<p>
Card 1
</p>
</card>
<card title='yes' id="a2" >
<p>
Card 2 $aa
</p> </card> </wml>
Lets understand the order
of execution. Using setvar, aa gets the value of 1ab and then <go> gets
executed. The setvar gets executed before go. Therefore $aa displays the value
as 1ab.
The next program adds some
more lines to the previous one. Card 1 remains the same. In the second card when you say
onenterforward, we once again have a setvar but it is within a refresh task.
Here we are setting aa to ‘def’.
|
Screen 3. 3 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' id="a1" >
<onevent type="onenterforward">
<go href="#a2">
<setvar name="aa" value="1ab"/>
</go>
</onevent>
<p>
Card 1
</p> </card>
<card title='yes' id="a2" >
<onevent type="onenterforward">
<refresh>
<setvar name="aa" value="def"/>
</refresh>
</onevent>
<p>
Card 2 $aa
</p>
</card>
</wml>
The output is shown in Fig
3.3. The bright idea of doing all this is to tell you that variables are
entities whose contents can change or vary.
In card1, aa had one value, in card2, it gets another value.
In the first card, instead
of using go, say refresh. Refresh clears the screen, i.e. it redraws the screen
and more important is that it is also refreshes the task. It is a task that
doesn't go anywhere. It is a case of <go> going to the same card. You can
set the values of a variable in the prev task too.
What happens when you want
to display the $ sign? Here is a problem. Since the browser considers the word
that follows the $ sign a variable, we have a problem. You will not get an
error, but you will also not get any output. So if you want to use the $, then
you add one more $. This is what comes from staying in capitalist societies too
long. If you don't tip a bit more, you do not get your work done. It's like two
wrongs make a right.
|
Screen 3. 4 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' id="a1" >
<p>
Card 1
You spend $$15
</p>
</card>
</wml>
Variable names are case sensitive.
This may sound like a silly statement to make, but it is true. No, the browser
does not know the variable you have in mind, but whatever you define and
however you define it, you must follow the same case later on as well. Hence,
$aa, $Aa, $aA, and $AA are different variables, even though they look similar.
|
Screen 3. 5 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card >
<onevent type="onenterforward">
<refresh>
<setvar name="bb" value="10"/>
</refresh>
</onevent>
<p>
Card $bb
Card $BB
</p>
</card>
</wml>
The documentation tells you
very clearly that you can either display the variable as $aa, or $(aa). The
variation in the usage style is for variable names that have spaces. Since you
know that the variable names need to be matched in case, it should not be
difficult to understand that they have to match otherwise as well.
|
Screen 3. 6 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card >
<onevent type="onenterforward">
<refresh>
<setvar name="aa " value="10"/>
</refresh>
</onevent>
<p>
Card $(aa)
</p>
</card>
</wml>
Defining a variable name a a, is okay. You will not get an error for this. However, $a a does not display the correct output as the variable is not recognised. $a is not defined explicitly.
|
Screen 3. 7 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card >
<onevent type="onenterforward">
<refresh>
<setvar name="a a" value="10"/>
</refresh>
</onevent>
<p>
Card $(a)
</p>
</card>
</wml>
|
Screen 3. 8 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="card1" title="Title">
<onevent type="onenterforward" >
<refresh>
<setvar name="aa" value="h:.-_ bye"/>
</refresh>
</onevent>
<p>
V:$(aa:escape) <br/>
V:$(aa:unesc) <br/>
V:$(aa:noesc) <br/>
</p>
</card>
</wml>
The display screen shows the
escape characters for a colon : and space. aa is initialised to h:.-_ a space
and then bye. aa:escape will use the escape characters for the characters it
understands. unesc and noesc will not escape any characters. A useful method
where we want to display %20 as in %20 and not space.
The next example shows you 4 different ways of displaying the same variable. The variable used is X, and within <p> and </p> you display the value using different combinations.
|
Screen 3. 9 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="card1" title="Title">
<onevent type="onenterforward" >
<refresh>
<setvar name="X" value="h"/>
</refresh>
</onevent>
<p>
allstand for same $X <br/>
$X <br/>
$X <br/>
$X <br/>
$X <br/>
</p>
</card>
</wml>
& is an entity. After the
#, if you have an x, the number following it is treated as a hexadecimal
number. Actual numbers are not preceded by x. hex 24 is an ascii equivalent for
$. In normal decimal numbers the value is 36. #x24 and #36 mean $. Similarly 58
is the hex number for the ascii character X. #x58 will evaluate to X. &
evaluates the expression and the browser, on receiving the statement as $X,
displays hi on the screen.
Timer
A related topic that we
need to consider is timers. Now, we have come to another section of this
chapter which relates to time. Whenever we work with a computer, we would like
to know when some amount of time has elapsed and accordingly we want something
to happen. Like for e.g. it happens to
us all the time, when we work with a computer and after 10 seconds have passed
without we touching it, the
screensaver takes over. So to get effects like this we have to have
another event which works with time. As we mentioned earlier, there are 2
events, onenterforward and onenterbackward. These are called intrinsic events.
There is one more event called the ontimer.
In the ontimer event, you
go to a certain card after some time has elapsed. But with an ontimer, you must have a timer suboption and in our
program we have given the timer a value of 100. That means after 10 seconds, it will go to a2.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' id="a1" ontimer="#a2" >
<timer value="100"/>
<p>
Card 1
</p>
</card>
<card title='yes' id="a2" >
<p>
Card 2
</p>
</card>
</wml>
|
|
Ontimer takes you to
another location. In the next example, we have 3 cards. The first card takes you to a2, second card takes
you to a3, and the third one takes you back to a1. This is useful in displaying animated pictures.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="a1" ontimer="#a2" >
<timer value="10"/>
<p>
Card 1
</p>
</card>
<card id="a2" ontimer="#a3" >
<timer value="10"/>
<p>
Card 2
</p>
</card>
<card id="a3" ontimer="#a1" >
<timer value="10"/>
<p>
Card 3
</p>
</card>
</wml>
|
|
|
Screen 3. 12 |
Screen 3. 13 |
Screen 3. 14 |
Now you will ask one
question as to why did we learn variables?
Well for many reasons. One reason is to send stuff over, the second
reason is to accept data from the user, which we will learn in the next
chapter. And the third reason is explained through an example given below.
Along with the timer we
have the name attribute initialised to aa. We are not displaying any value
here. After 10 seconds, you will see the next card i.e. Card 2 on your screen.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' id="a1" ontimer="#a2" >
<timer value="100" name="aa"/>
<p>
Card 1
</p>
</card>
<card title='no' id="a2" >
<p>
Card 2
</p>
</card>
</wml>
|
|
We now apply the same old rule,
that says before starting our program, initialise the variable.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' id="a1" ontimer="#a2" >
<onevent type="onenterforward">
<refresh>
<setvar name="aa" value="10"/>
</refresh>
</onevent>
<timer value="100" name="aa"/>
<p>
Card 1
</p>
</card>
<card title='yes' id="a2" >
<p>
Card 2 $aa
</p>
</card>
</wml>
|
|
We use onenterforward and have
the refresh task. Within refresh we have given the name aa a value of 10, so
within 1 second the timer will get activated. With the timer, if you have a
name and a timer value, then the setvar name value has precedence over the
timer value.
If the name is unset then
the timer value is seen. This is a
problem with XML. If you have a timer, then
name is optional but the value is mandatory. The timer value is a must, the name is optional. But if you have set a name with a value then
the timer value never gets seen.
Now lets take the next
example.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' id="a1" ontimer="#a2" >
<timer value="100"/>
<do type="aaa">
<refresh />
</do>
<p>
Card 1
</p>
</card>
<card title='yes' id="a2" >
<p>
Card 2 $aa
</p>
</card>
</wml>
|
|
Here we have a do and a
timer. When you click on Options, you will see the same screen but after 10 seconds
you will automatically be taken to the next screen. This implies that the
refresh in do does not refresh the timer.
The next example proves
that WML is not like any programming language.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' id="a1" ontimer="#a2" >
<onevent type="onenterforward">
<refresh>
<setvar name="aa" value="200"/>
</refresh>
</onevent>
<timer value="100" name="aa"/>
<do type="asd"label=”new”>
<go href="#a2"/>
</do>
<p>
Card 1
</p>
</card>
<card title='yes' id="a2" >
<p>
Card 2 $aa
</p>
</card>
</wml>
|
|
|
Screen 3. 21 |
Screen 3. 22 |
Screen 3. 23 |
This program has a card with onevent, onenterforward and a
timer. Now we also have a do that takes us to card2. The timer and the go,
both, will take us to card2. The variable aa has been initialized to 200 thus
the timer value will be ignored.
Once 20 seconds have elapsed, we will be taken to the second card with the value of aa as 0. Now reload the program and while you are waiting, after 5 seconds, click on Options. Move on to card2 and you will now see a different value. It doesn't remain 0. Aa will display a value in the range of 0 and 200.