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:


(ontimerEg1.wml)

<?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


Feedback Form (ExpandCollapse)

What do you think about this web page?






(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Please enter again to confirm:

Due to the amount of messages we received, we may not be able to reply to all messages.

A button for going back to the top of this page