PURE example - Reuse a template and JSONP load Revision 353037373063 (Mon Nov 22 2010 at 03:15) - Diff Link to this snippet: https://friendpaste.com/6TxTxnXf9sPl53lB6ym6sM Embed: manni perldoc borland colorful default murphy trac fruity autumn bw emacs pastie friendly Show line numbers Wrap lines 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Reuse a compiled template - Show timezone current time</title> <script src="../libs/sizzle.js"></script> <script src="../libs/pure2.js"></script></head><body> <div id="clock"> It is now <span class="hour"></span> : <span class="minute"></span> : <span class="second"></span><br /> In <span class="tz"></span> </div> <p><a href="#" onclick="clearTimeout(_to);return false;">stop</a></p> <script> (function(){ var // Get the html source cross lib, using $p // adapt it to your library, i.e: $( '#clock' ) in jQuery html = $p( '#clock' ), // json service returning the current time for a timezone tz = 'Europe/Brussels', url = 'http://json-time.appspot.com/time.json?tz='+tz+'&callback=showTime&cache=', //directive to render the template directive = { 'span.hour': overlay('hour'), 'span.minute': overlay('minute'), 'span.second': overlay('second'), 'span.tz': 'tz' }, // compile the template once template = html.compile( directive ); // utility fn to add leading 0 to numbers function overlay(what){ return function(a){ var val = a.context[what]; return val === 0 ? '00' : val < 10 ? '0' + val : val; }; } // JSONP load - script injection with callback function var noCache = 0; function loadTime(){ var old = document.getElementById('dataLoad'), s = document.createElement("script"); s.src = url + noCache++; !old ? document.body.appendChild(s) : document.body.replaceChild(s, old); s.id = 'dataLoad'; } // callback function to Render the template window.showTime = function(data){ // rendering but reusing the compiled function template html = html.render( data, template ); // redo it every sec window._to = setTimeout( loadTime, 1000 ); }; // Call the time service loadTime(); }()); </script></body></html>