Tutorials
Develop
This tutorial will assist you with the steps
of the widget development process using a simple widget.


Step 1: Preparation
Step 2: Basic Widget Functionality
Step 3: Advanced Applications

Lesson 6: Using a Timer

Let's study how to use a timer. When the mylo® Screen is displayed the foreground timer will start to run and is set to reoccur every 1 minute. When the mylo® Screen is displayed the background timer will run on 5 minute intervals, but only when the network connection is active. When the OPTION menu item "Refresh" is selected, the background timer will be activated. Please verify that the banner image is changed when "Refresh" is selected.


Download Sample5.mylow from here


myloConfig.xml
The banner image update interval should be fairly straightforward and is measured in milliseconds.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2008 Sony Electronics Inc. -->
<config xmlns="http://xmlns.sony.net/mylo/widget" version="1.0">
	<item name="jumpURL" type="text" visible="off">
		<value>http://www.google.com</value>
	</item>
	<item title="Update Interval" name="frequency" type="select" comment="Set the banner update interval">
		<option value="60000">1 Minute</option>
		<option value="300000">5 Minutes</option>
		<option value="600000">10 Minutes</option>
		<option value="1800000">30 Minutes</option>
		<option value="3600000">1 Hour</option>
		<option value="0">Do Not Update</option>
		<value>1 Minute</value>
		<default>1 Minute</default>
	</item>
</config>

Code.js
Since the desired value has already been selected in preferences, we will try to read in the same value in options. Then we will set the same value in both the foreground and the background timer, and execute the same process on the interval of the timer.

/*
 * Copyright 2008 Sony Electronics Inc.
 */

var extension = new Extension();
var jumpURL;
var frequency = 0;
var numImage = 4;
var currentImg = 0;
var foregroundTimer;
var backgroundTimer;

/**
 * Initialization
 */
var init = function() {
	prefObject = new Preferences(prefCallback);
}

/**
 * Acquire preferences
 */
var prefCallback = function() {
	var _i;
	var _frequency;
	var _prevTimer;

	_prevTimer = frequency;
	jumpURL = prefObject.getValueByName('jumpURL');
	_frequency = prefObject.getItemByName('frequency');
	for (_i = 0; _i < _frequency.options.length; _i++) {
		if (prefObject.getValueByName('frequency') == _frequency.options[_i].text) {
			frequency = parseInt(_frequency.options[_i].value);
		}
	}
	if (_prevTimer != frequency) {
		unsetTimers();
		if (frequency > 0) {
			setTimers();
		}
	}
	notifyReadyWidget();
	return prefObject;
};

/**
 * When the preference starts, run the callback
 */
var getPreferenceObject = function() {
	return prefObject;
}

/**
 * When the preference ends, run the callback
 */
var changePreference = function(prefObject, updateFlag) {
	if (updateFlag == true){
		extension.saveFile(Extension.fileType.CONFIG, saveCallback, prefObject.save());
		prefCallback();
	}
	return;
}

/**
 * When the preferences are saved, run the callback
 */
var saveCallback = function() {};

/**
 * Set the timer
 */
var unsetTimers = function() {
	if(foregroundTimer) {
		clearInterval(foregroundTimer);
		foregroundTimer = undefined;
	}
	if(backgroundTimer) {
		clearBackgroundInterval(backgroundTimer);
		backgroundTimer = undefined;
	}
};

/**
 * Set the timers
 */
var setTimers = function() {
	unsetTimers();
	foregroundTimer = setInterval(callbackForegroundTimer, frequency);
	backgroundTimer = setBackgroundInterval(callbackBackgroundTimer, frequency);
};

/**
 * Foreground timer arrangement/process
 */
var callbackForegroundTimer = function() {
	redrawBanner();
};

/**
 * Background timer arrangement/process
 */
var callbackBackgroundTimer = function() {
	redrawBanner();
};

/**
 * Redraw banner
 */
var redrawBanner = function() {
	if (++currentImg >= numImage) {
		currentImg = 0;
	}
	switch (currentImg) { 
        case 0: 
            baseImg.src = 'images/google1.png'; 
        break; 
        case 1: 
            baseImg.src = 'images/google2.png'; 
        break; 
        case 2: 
            baseImg.src = 'images/google3.png'; 
        break; 
        case 3: 
            baseImg.src = 'images/google4.png'; 
        break; 
    }
};

/**
 * Key handler
 */
var widgetKeyDown  = function(evt, key) {
	switch (key) {
	case mylo.KeyCode.EXE:
		extension.openWeb(jumpURL);
		return false;
		break;
	case mylo.KeyCode.BACK:
		return true;
		break;
	}
}

Go back to the top of the page

Return to Tutorial