Responsive interfaces

36
Responsive Interfaces Nicholas C. Zakas Principal Front-end Engineer, Yahoo! Homepage Bayjax – April 13, 2010

Transcript of Responsive interfaces

Page 1: Responsive interfaces

Responsive Interfaces

Nicholas C. ZakasPrincipal Front-end Engineer,Yahoo! Homepage

Bayjax – April 13, 2010

Page 2: Responsive interfaces

The UI Thread

• Two jobs:– Draw UI updates– Execute JavaScript

• Only one job can happen at a time

• Jobs are added to a queue if they are generated while the UI thread is busy

Page 3: Responsive interfaces

<button id="btn" style="font-size: 30px; padding: 0.5em 1em">Click Me</button>

<script type="text/javascript">

window.onload = function(){

document.getElementById("btn").onclick = function(){

//do something

};

};

</script>

Page 4: Responsive interfaces

When Clicked

• A button UI change job is created– Draws the button in the down state

• A JavaScript execution job is created– The onclick event handler

• A button UI change job is created– Draws the button in the up state

Page 5: Responsive interfaces

Before Click

Start UI Thread

UI Queue

Page 6: Responsive interfaces

When Clicked

Start UI Thread

onclick

UI Queue

Page 7: Responsive interfaces

When Clicked

Start UI Thread

onclick

UI Queue

Page 8: Responsive interfaces

When Clicked

onclick

Start UI Thread

UI Queue

Page 9: Responsive interfaces

When Clicked

onclick

Start UI Thread

UI Queue

Page 10: Responsive interfaces

No UI updates while JavaScript is executing!

Page 11: Responsive interfaces

Why?

Page 12: Responsive interfaces

JavaScript May Cause UI Update

<button id="btn" style="font-size: 30px; padding: 0.5em 1em">Click Me</button>

<script type="text/javascript">

window.onload = function(){

document.getElementById("btn").onclick = function(){

var div = document.createElement(“div”);

div.className = “tip”;

div.innerHTML = “You clicked me!”;

document.body.appendChild(div);

};

};

</script>

Page 13: Responsive interfaces

UI updates must use the latest info available

Page 14: Responsive interfaces

Long-running JavaScript

=

Unresponsive UI

Page 15: Responsive interfaces

Runaway Script Timer• Designed to prevent the browser from affecting

the operating system

• Limits the amount of time a script is allowed to execute

• Two types of limits:– Execution time

– Number of statements

• Always pops up a scary dialog to the user

• Exception: Opera has no runaway timer

Page 16: Responsive interfaces

Internet Explorer

Page 17: Responsive interfaces

Firefox

Page 18: Responsive interfaces

Safari

Page 19: Responsive interfaces

Chrome

Page 20: Responsive interfaces

Runaway Script Timer Limits• Internet Explorer: 5 million statements

• Firefox: 10 seconds

• Safari: 5 seconds

• Chrome: Unknown, hooks into normal crash control mechanism

• Opera: none

Page 21: Responsive interfaces

How long is too long?

Page 22: Responsive interfaces

How Long Is Too Long?

“0.1 second [100ms] is about the limit for having the user feel that the system is reacting instantaneously, meaning that no special feedback is necessary except to display the result.”

- Jakob Nielsen

Page 23: Responsive interfaces

Translation:

JavaScript should not execute for longer than 100ms to ensure

responsive UI

Page 24: Responsive interfaces

Recommendation:

Limit JavaScript to no more than 50ms at a time

Page 25: Responsive interfaces

function processArray(items, process, callback){

for (var i=0,len=items.length; i < len; i++){

process(items[i]);

}

callback();

}

Page 26: Responsive interfaces

Timers to the rescue!

Page 27: Responsive interfaces

JavaScript Timers

• Created using setTimeout()

• Schedules a new JavaScript execution job for some time in the future

• When the delay is up, the job is added to the UI queue– Note: This does not guarantee execution after

the delay, just that the job is added to the UI queue and will be executed when appropriate

Page 28: Responsive interfaces

JavaScript Timers

• For complex processing, split up into timed functionality

• Use timers to delay some processing for later

Page 29: Responsive interfaces

function timedProcessArray(items, process, callback){

//create a clone of the original var todo = items.concat();

setTimeout(function(){

var start = +new Date();

do {

process(todo.shift());

} while (todo.length > 0 &&

(+new Date() - start < 50));

if (todo.length > 0){

setTimeout(arguments.callee, 25);

} else {

callback(items);

}

}, 25);

}

Page 30: Responsive interfaces

Web Workers to the rescue!

Page 31: Responsive interfaces
Page 32: Responsive interfaces

Web Workers

• Asynchronous JavaScript execution

• Execution happens in a separate process– Not on the UI thread = no UI delays

• Data-driven API– Data is serialized when sending data into or out

of Worker– No access to DOM, BOM– Completely separate execution environment

Page 33: Responsive interfaces

//in pagevar worker = new Worker("code.js");

worker.onmessage = function(event){

alert(event.data);

};

worker.postMessage("Nicholas");

//in code.js

self.onmessage = function(event){

self.postMessage("Hello, " + event.data + "!");

};

Page 34: Responsive interfaces

Web Workers Support

• Firefox 3.5

• Safari 4

• Chrome 4

Page 35: Responsive interfaces

Summary

• UI thread is used both for JavaScript execution and UI updates

• UI cannot be updated while JavaScript is executing

• Keep JavaScript execution to 50ms – use timers and/or web workers to offload processing

Page 36: Responsive interfaces

Etcetera• My blog: www.nczonline.net

• My email: [email protected]

• Twitter: @slicknet