15. Timer and the ontimer Event
The ontimer event is used together with timers. To add a timer in WML, you need the <timer/> tag. Its value attribute specifies the period of time after which the timer will expire. The time unit is 1/10 second. For example, the following WML markup:
<timer value="10"/>
defines a timer that will expire after 1 second.
The ontimer event occurs when a timer expires and the WML code associated with the event will be executed.
Timers will be particularly useful to you if you want to develop a mobile Internet application that provides real-time information to users. You can add a timer to refresh the content of a card regularly so that the information on the card can be kept updated without involving any user actions. The following WML example demonstrates how to instruct the WAP browser to refresh a card automatically every 5 seconds:
<?xml
version="1.0"?>
<!DOCTYPE wml PUBLIC
"-//WAPFORUM//DTD WML 1.3//EN"
"http://www.wapforum.org/DTD/wml13.dtd">
<wml>
<card
id="card1" title="Timer in WML">
<onevent
type="ontimer">
<go
href="ontimerEg1.wml"/>
</onevent>
<timer
value="50"/>
<p>
WML
Timer Test
</p>
</card>
</wml>
In the above WML example, the markup:
<timer value="50"/>
defines a timer that will expire 5 seconds after the card is loaded.
The following markup defines the ontimer event handler:
<onevent
type="ontimer">
<go
href="ontimerEg1.wml"/>
</onevent>
When the ontimer event is raised, the WAP browser will load the ontimerEg1.wml file, which means the card is refreshed.
Note that many wireless devices are cache-enabled by default. This means during each refresh, the card displayed may be the cached version instead of the updated version requested from the WAP server. To solve this problem, you need to disable the cache. The way to do this depends on the actual wireless device. Some wireless devices require that the "Cache-Control: no-cache" header line to be specified in the HTTP response, while some others support the use of the <meta> element to specify the time-to-live (TTL) period of a WML document in the cache (this has been discussed in the earlier section "WML Generic Metadata: Cache Control" of this WML tutorial).
The WML code below:
<card
id="card1" title="Timer in WML">
<onevent
type="ontimer">
<go
href="ontimerEg1.wml"/>
</onevent>
<timer
value="50"/>
<p>
...
</p>
</card>
can be written in the following short form. The ontimer attribute of the <card> tag is used instead of the <onevent></onevent> and <go/> tags.
<card
id="card1" title="Timer in WML"
ontimer="ontimerEg1.wml">
<timer
value="50"/>
<p>
...
</p>
</card>
Note that the order of the <timer> element and the <onevent> element in the WML document does matter. The following block of markup code cannot pass through the validator:
<card
id="card1" title="Timer in WML">
<timer
value="50"/>
<onevent type="ontimer">
<go
href="ontimerEg1.wml"/>
</onevent>
<p>
...
</p>
</card>
Previous Page | Page 28 of 50 | Next Page |
- 1. WML (Wireless Markup Language) Introduction
- 2. WML Deck and Card
- 3. WML Document Structure
- 4. WML Generic Metadata: <meta>
- 5. Comments in WML
- 6. Line Breaking in WML
- 7. Font Size and Style in WML
- 8. WML Preformatted Text: <pre>
- 9. WML Images
- 10. WML Tables
- 11. WML Anchor Links
- 12. Programming Softkeys of Mobile Phones and the <do> Element
- 13. WML Template and Menu
- 14. WML Events and the <onevent> Tag
- 15. Timer and the ontimer Event
- 16. WML Event: onenterbackward
- 17. WML Event: onenterforward
- 18. WML Selection Lists and the onpick Event
- 19. WML Input Fields
- 20. WML Variables
- 21. Submitting Form Data to the Server in WML
- 22. Clearing a Saved Form in WML