Example
The following sample Klip is the same as the WMI Klip in Engines.Platform, with addition of a timer to check memory once every two seconds instead of every minute.Note: Because this Klip contains script that may be harmful to your computer, it will be blocked. To unblock it, select "Allow Blocked Script to Run" while you are in developer mode.
<klip>
<identity>
<title>
API: Timer Klip
</title>
</identity>
<locations>
<icon>
http://www.klipfolio.com/static/klips/klipfolio/sample_icon.png
</icon>
<banner>
http://www.klipfolio.com/static/klips/klipfolio/sample_banner.png
</banner>
</locations>
<setup>
<scriptmode>
extended
</scriptmode>
<enterprise>
true
</enterprise>
</setup>
<messages>
<loading>
Getting Memory Usage...
</loading>
</messages>
<style>
item {
type:item
}
mempercent {
itemcol: 1;
noterow:1;
label:"Percentage";
}
memused {
itemcol: 2;
noterow:2;
label:"Used";
}
memtotal {
itemcol: 3;
noterow:3;
label:"Available";
}
</style>
<klipscript>
<![CDATA[
// Create a timer for callback every 2000 miliseconds (2 seconds)
var g_timer = createTimer (2000);
// Call getMemData() each tick of the timer
g_timer.onTick = getMemData;
function onLoad ()
{
Items.savehistory = false;
Items.canalert = false;
Items.autoremove = false;
}
function getMemData ()
{
var freekbytes = Engines.Platform.getAvailableKBytes ();
var totalbytes = Engines.Platform.queryWMI ("SELECT * FROM Win32_PhysicalMemory", "Capacity");
if (freekbytes.length && totalbytes.length)
{
var memarray = totalbytes.split (",");
var i;
var totalkbytes = 0;
for (i = 0; i < memarray.length; i++)
{
totalkbytes += memarray[i] - 0;
}
totalkbytes = totalkbytes / 1024;
var mempct = Math.round ((totalkbytes - freekbytes)/ totalkbytes * 100);
var xml = "<item><mempercent>" + mempct + "%</membercent>" +
"<memused>" + (totalkbytes - freekbytes) + " KB" +
"</memused><memtotal>" + freekbytes + " KB" +
"</memtotal></item>";
Items.clear( true );
Engines.Data.process (xml);
}
return true;
}
function onRefresh ()
{
var success = getMemData();
// Make sure the user can't delete or dim (visit) the webcam image
if(Items.length)
{
Items[0].canvisit = false;
Items[0].candelete = false;
}
return success;
}
]]>
</klipscript>
</klip>
The above Klip is the same WMI Klip with the addition of two lines at the top of the Klip's JavaScript to define the timer.
// Create a timer for callback every 2000 milliseconds (2 seconds) var g_timer = createTimer (2000); // Call getMemData() each tick of the timer g_timer.onTick = getMemData;
Timers give you the ability to make Klips that update the displayed webcam image, and the
system monitor Klips that use the timer to keep the system information displays up to date.
These are not the only ways a timer can be used, though, and we expect Klip developers will come
up with some other clever and innovative ideas for timer-based Klips.
| Properties Summary | |
String |
onTick
Specifies a function to call every timer interval. |
| Properties Detail |
onTick
String onTick
-
Specifies a function to call every timer interval.
To add a timer to your Klip, create a global timer variable at the top of your JavaScript block:
var gTimer = createTimer(5000);
The argument passed to Klip.createTimer() is specified in milliseconds, so 5000 means the timer will tick every 5 seconds. The only other thing you need define is a callback function for your timer:
gTimer.onTick = myTimerFunction;
function myTimerFunction()
{
// Respond to timer code here.
// This function does not need to have a return call.
}
The callback function (myTimerFunction in the example above) will be called every time the timer ticks.
Note: The timer callback function is just like any other JavaScript function in your Klip, and has access to the Klip's preferences and Items array. The only limitation is that the timer callback does not count as an Klip.onRefresh() event, so it cannot create HTTP connections. However, you can call Engines.Data.process() if you have XML to process (such as in refreshing a web-cam Klip faster than once a minute). Engines.Data.process() will update any images defined in the Klip's CSS and referenced in the XML.
|
Klipfolio Dashboard 5 API | ||||||||
| PREV OBJECT NEXT OBJECT | FRAMES NO FRAMES | ||||||||
| SUMMARY: PROPERTY | FUNCTION | DETAIL: PROPERTY | FUNCTION | ||||||||
© 2009 Klipfolio Inc. All Rights Reserved.
