Creating Custom Events

You can create and listen to custom application events. You can make up any type of event that you please.

For example, if you are creating a game, you might want to create a heartbeat event that is raised every second so that you can refresh the game board. The following code demonstrates how you can raise and listen to a custom heartbeat event:

window.setInterval(function () {    WinJS.Application.queueEvent({ type: "heartbeat" });}, 1000);WinJS.Application.addEventListener("heartbeat", function (evt) {    console.log("heartbeat");});WinJS.Application.start();

The preceding code uses the setInterval() method to execute code once every second. The queueEvent() method is called to queue up ...

Get Windows® 8.1 Apps with HTML5 and JavaScript Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.