notes.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>reveal.js - Slide Notes</title>
  6. <style>
  7. body {
  8. font-family: Helvetica;
  9. font-size: 18px;
  10. }
  11. #current-slide,
  12. #upcoming-slide,
  13. #speaker-controls {
  14. padding: 6px;
  15. box-sizing: border-box;
  16. -moz-box-sizing: border-box;
  17. }
  18. #current-slide iframe,
  19. #upcoming-slide iframe {
  20. width: 100%;
  21. height: 100%;
  22. border: 1px solid #ddd;
  23. }
  24. #current-slide .label,
  25. #upcoming-slide .label {
  26. position: absolute;
  27. top: 10px;
  28. left: 10px;
  29. z-index: 2;
  30. }
  31. .overlay-element {
  32. height: 34px;
  33. line-height: 34px;
  34. padding: 0 10px;
  35. text-shadow: none;
  36. background: rgba( 220, 220, 220, 0.8 );
  37. color: #222;
  38. font-size: 14px;
  39. }
  40. .overlay-element.interactive:hover {
  41. background: rgba( 220, 220, 220, 1 );
  42. }
  43. #current-slide {
  44. position: absolute;
  45. width: 60%;
  46. height: 100%;
  47. top: 0;
  48. left: 0;
  49. padding-right: 0;
  50. }
  51. #upcoming-slide {
  52. position: absolute;
  53. width: 40%;
  54. height: 40%;
  55. right: 0;
  56. top: 0;
  57. }
  58. /* Speaker controls */
  59. #speaker-controls {
  60. position: absolute;
  61. top: 40%;
  62. right: 0;
  63. width: 40%;
  64. height: 60%;
  65. overflow: auto;
  66. font-size: 18px;
  67. }
  68. .speaker-controls-time.hidden,
  69. .speaker-controls-notes.hidden {
  70. display: none;
  71. }
  72. .speaker-controls-time .label,
  73. .speaker-controls-pace .label,
  74. .speaker-controls-notes .label {
  75. text-transform: uppercase;
  76. font-weight: normal;
  77. font-size: 0.66em;
  78. color: #666;
  79. margin: 0;
  80. }
  81. .speaker-controls-time, .speaker-controls-pace {
  82. border-bottom: 1px solid rgba( 200, 200, 200, 0.5 );
  83. margin-bottom: 10px;
  84. padding: 10px 16px;
  85. padding-bottom: 20px;
  86. cursor: pointer;
  87. }
  88. .speaker-controls-time .reset-button {
  89. opacity: 0;
  90. float: right;
  91. color: #666;
  92. text-decoration: none;
  93. }
  94. .speaker-controls-time:hover .reset-button {
  95. opacity: 1;
  96. }
  97. .speaker-controls-time .timer,
  98. .speaker-controls-time .clock {
  99. width: 50%;
  100. }
  101. .speaker-controls-time .timer,
  102. .speaker-controls-time .clock,
  103. .speaker-controls-time .pacing .hours-value,
  104. .speaker-controls-time .pacing .minutes-value,
  105. .speaker-controls-time .pacing .seconds-value {
  106. font-size: 1.9em;
  107. }
  108. .speaker-controls-time .timer {
  109. float: left;
  110. }
  111. .speaker-controls-time .clock {
  112. float: right;
  113. text-align: right;
  114. }
  115. .speaker-controls-time span.mute {
  116. opacity: 0.3;
  117. }
  118. .speaker-controls-time .pacing-title {
  119. margin-top: 5px;
  120. }
  121. .speaker-controls-time .pacing.ahead {
  122. color: blue;
  123. }
  124. .speaker-controls-time .pacing.on-track {
  125. color: green;
  126. }
  127. .speaker-controls-time .pacing.behind {
  128. color: red;
  129. }
  130. .speaker-controls-notes {
  131. padding: 10px 16px;
  132. }
  133. .speaker-controls-notes .value {
  134. margin-top: 5px;
  135. line-height: 1.4;
  136. font-size: 1.2em;
  137. }
  138. /* Layout selector */
  139. #speaker-layout {
  140. position: absolute;
  141. top: 10px;
  142. right: 10px;
  143. color: #222;
  144. z-index: 10;
  145. }
  146. #speaker-layout select {
  147. position: absolute;
  148. width: 100%;
  149. height: 100%;
  150. top: 0;
  151. left: 0;
  152. border: 0;
  153. box-shadow: 0;
  154. cursor: pointer;
  155. opacity: 0;
  156. font-size: 1em;
  157. background-color: transparent;
  158. -moz-appearance: none;
  159. -webkit-appearance: none;
  160. -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  161. }
  162. #speaker-layout select:focus {
  163. outline: none;
  164. box-shadow: none;
  165. }
  166. .clear {
  167. clear: both;
  168. }
  169. /* Speaker layout: Wide */
  170. body[data-speaker-layout="wide"] #current-slide,
  171. body[data-speaker-layout="wide"] #upcoming-slide {
  172. width: 50%;
  173. height: 45%;
  174. padding: 6px;
  175. }
  176. body[data-speaker-layout="wide"] #current-slide {
  177. top: 0;
  178. left: 0;
  179. }
  180. body[data-speaker-layout="wide"] #upcoming-slide {
  181. top: 0;
  182. left: 50%;
  183. }
  184. body[data-speaker-layout="wide"] #speaker-controls {
  185. top: 45%;
  186. left: 0;
  187. width: 100%;
  188. height: 50%;
  189. font-size: 1.25em;
  190. }
  191. /* Speaker layout: Tall */
  192. body[data-speaker-layout="tall"] #current-slide,
  193. body[data-speaker-layout="tall"] #upcoming-slide {
  194. width: 45%;
  195. height: 50%;
  196. padding: 6px;
  197. }
  198. body[data-speaker-layout="tall"] #current-slide {
  199. top: 0;
  200. left: 0;
  201. }
  202. body[data-speaker-layout="tall"] #upcoming-slide {
  203. top: 50%;
  204. left: 0;
  205. }
  206. body[data-speaker-layout="tall"] #speaker-controls {
  207. padding-top: 40px;
  208. top: 0;
  209. left: 45%;
  210. width: 55%;
  211. height: 100%;
  212. font-size: 1.25em;
  213. }
  214. /* Speaker layout: Notes only */
  215. body[data-speaker-layout="notes-only"] #current-slide,
  216. body[data-speaker-layout="notes-only"] #upcoming-slide {
  217. display: none;
  218. }
  219. body[data-speaker-layout="notes-only"] #speaker-controls {
  220. padding-top: 40px;
  221. top: 0;
  222. left: 0;
  223. width: 100%;
  224. height: 100%;
  225. font-size: 1.25em;
  226. }
  227. @media screen and (max-width: 1080px) {
  228. body[data-speaker-layout="default"] #speaker-controls {
  229. font-size: 16px;
  230. }
  231. }
  232. @media screen and (max-width: 900px) {
  233. body[data-speaker-layout="default"] #speaker-controls {
  234. font-size: 14px;
  235. }
  236. }
  237. @media screen and (max-width: 800px) {
  238. body[data-speaker-layout="default"] #speaker-controls {
  239. font-size: 12px;
  240. }
  241. }
  242. </style>
  243. </head>
  244. <body>
  245. <div id="current-slide"></div>
  246. <div id="upcoming-slide"><span class="overlay-element label">Upcoming</span></div>
  247. <div id="speaker-controls">
  248. <div class="speaker-controls-time">
  249. <h4 class="label">Time <span class="reset-button">Click to Reset</span></h4>
  250. <div class="clock">
  251. <span class="clock-value">0:00 AM</span>
  252. </div>
  253. <div class="timer">
  254. <span class="hours-value">00</span><span class="minutes-value">:00</span><span class="seconds-value">:00</span>
  255. </div>
  256. <div class="clear"></div>
  257. <h4 class="label pacing-title" style="display: none">Pacing – Time to finish current slide</h4>
  258. <div class="pacing" style="display: none">
  259. <span class="hours-value">00</span><span class="minutes-value">:00</span><span class="seconds-value">:00</span>
  260. </div>
  261. </div>
  262. <div class="speaker-controls-notes hidden">
  263. <h4 class="label">Notes</h4>
  264. <div class="value"></div>
  265. </div>
  266. </div>
  267. <div id="speaker-layout" class="overlay-element interactive">
  268. <span class="speaker-layout-label"></span>
  269. <select class="speaker-layout-dropdown"></select>
  270. </div>
  271. <script src="../../plugin/markdown/marked.js"></script>
  272. <script>
  273. (function() {
  274. var notes,
  275. notesValue,
  276. currentState,
  277. currentSlide,
  278. upcomingSlide,
  279. layoutLabel,
  280. layoutDropdown,
  281. pendingCalls = {},
  282. lastRevealApiCallId = 0,
  283. connected = false;
  284. var SPEAKER_LAYOUTS = {
  285. 'default': 'Default',
  286. 'wide': 'Wide',
  287. 'tall': 'Tall',
  288. 'notes-only': 'Notes only'
  289. };
  290. setupLayout();
  291. window.addEventListener( 'message', function( event ) {
  292. var data = JSON.parse( event.data );
  293. // The overview mode is only useful to the reveal.js instance
  294. // where navigation occurs so we don't sync it
  295. if( data.state ) delete data.state.overview;
  296. // Messages sent by the notes plugin inside of the main window
  297. if( data && data.namespace === 'reveal-notes' ) {
  298. if( data.type === 'connect' ) {
  299. handleConnectMessage( data );
  300. }
  301. else if( data.type === 'state' ) {
  302. handleStateMessage( data );
  303. }
  304. else if( data.type === 'return' ) {
  305. pendingCalls[data.callId](data.result);
  306. delete pendingCalls[data.callId];
  307. }
  308. }
  309. // Messages sent by the reveal.js inside of the current slide preview
  310. else if( data && data.namespace === 'reveal' ) {
  311. if( /ready/.test( data.eventName ) ) {
  312. // Send a message back to notify that the handshake is complete
  313. window.opener.postMessage( JSON.stringify({ namespace: 'reveal-notes', type: 'connected'} ), '*' );
  314. }
  315. else if( /slidechanged|fragmentshown|fragmenthidden|paused|resumed/.test( data.eventName ) && currentState !== JSON.stringify( data.state ) ) {
  316. window.opener.postMessage( JSON.stringify({ method: 'setState', args: [ data.state ]} ), '*' );
  317. }
  318. }
  319. } );
  320. /**
  321. * Asynchronously calls the Reveal.js API of the main frame.
  322. */
  323. function callRevealApi( methodName, methodArguments, callback ) {
  324. var callId = ++lastRevealApiCallId;
  325. pendingCalls[callId] = callback;
  326. window.opener.postMessage( JSON.stringify( {
  327. namespace: 'reveal-notes',
  328. type: 'call',
  329. callId: callId,
  330. methodName: methodName,
  331. arguments: methodArguments
  332. } ), '*' );
  333. }
  334. /**
  335. * Called when the main window is trying to establish a
  336. * connection.
  337. */
  338. function handleConnectMessage( data ) {
  339. if( connected === false ) {
  340. connected = true;
  341. setupIframes( data );
  342. setupKeyboard();
  343. setupNotes();
  344. setupTimer();
  345. }
  346. }
  347. /**
  348. * Called when the main window sends an updated state.
  349. */
  350. function handleStateMessage( data ) {
  351. // Store the most recently set state to avoid circular loops
  352. // applying the same state
  353. currentState = JSON.stringify( data.state );
  354. // No need for updating the notes in case of fragment changes
  355. if ( data.notes ) {
  356. notes.classList.remove( 'hidden' );
  357. notesValue.style.whiteSpace = data.whitespace;
  358. if( data.markdown ) {
  359. notesValue.innerHTML = marked( data.notes );
  360. }
  361. else {
  362. notesValue.innerHTML = data.notes;
  363. }
  364. }
  365. else {
  366. notes.classList.add( 'hidden' );
  367. }
  368. // Update the note slides
  369. currentSlide.contentWindow.postMessage( JSON.stringify({ method: 'setState', args: [ data.state ] }), '*' );
  370. upcomingSlide.contentWindow.postMessage( JSON.stringify({ method: 'setState', args: [ data.state ] }), '*' );
  371. upcomingSlide.contentWindow.postMessage( JSON.stringify({ method: 'next' }), '*' );
  372. }
  373. // Limit to max one state update per X ms
  374. handleStateMessage = debounce( handleStateMessage, 200 );
  375. /**
  376. * Forward keyboard events to the current slide window.
  377. * This enables keyboard events to work even if focus
  378. * isn't set on the current slide iframe.
  379. *
  380. * Block F5 default handling, it reloads and disconnects
  381. * the speaker notes window.
  382. */
  383. function setupKeyboard() {
  384. document.addEventListener( 'keydown', function( event ) {
  385. if( event.keyCode === 116 || ( event.metaKey && event.keyCode === 82 ) ) {
  386. event.preventDefault();
  387. return false;
  388. }
  389. currentSlide.contentWindow.postMessage( JSON.stringify({ method: 'triggerKey', args: [ event.keyCode ] }), '*' );
  390. } );
  391. }
  392. /**
  393. * Creates the preview iframes.
  394. */
  395. function setupIframes( data ) {
  396. var params = [
  397. 'receiver',
  398. 'progress=false',
  399. 'history=false',
  400. 'transition=none',
  401. 'autoSlide=0',
  402. 'backgroundTransition=none'
  403. ].join( '&' );
  404. var urlSeparator = /\?/.test(data.url) ? '&' : '?';
  405. var hash = '#/' + data.state.indexh + '/' + data.state.indexv;
  406. var currentURL = data.url + urlSeparator + params + '&postMessageEvents=true' + hash;
  407. var upcomingURL = data.url + urlSeparator + params + '&controls=false' + hash;
  408. currentSlide = document.createElement( 'iframe' );
  409. currentSlide.setAttribute( 'width', 1280 );
  410. currentSlide.setAttribute( 'height', 1024 );
  411. currentSlide.setAttribute( 'src', currentURL );
  412. document.querySelector( '#current-slide' ).appendChild( currentSlide );
  413. upcomingSlide = document.createElement( 'iframe' );
  414. upcomingSlide.setAttribute( 'width', 640 );
  415. upcomingSlide.setAttribute( 'height', 512 );
  416. upcomingSlide.setAttribute( 'src', upcomingURL );
  417. document.querySelector( '#upcoming-slide' ).appendChild( upcomingSlide );
  418. }
  419. /**
  420. * Setup the notes UI.
  421. */
  422. function setupNotes() {
  423. notes = document.querySelector( '.speaker-controls-notes' );
  424. notesValue = document.querySelector( '.speaker-controls-notes .value' );
  425. }
  426. function getTimings( callback ) {
  427. callRevealApi( 'getSlidesMetaInfo', [], function ( slides ) {
  428. callRevealApi( 'getConfig', [], function ( config ) {
  429. var defaultTiming = config.defaultTiming;
  430. if (defaultTiming == null) {
  431. callback(null);
  432. return;
  433. }
  434. var timings = [];
  435. for ( var i in slides ) {
  436. var slide = slides[ i ];
  437. var timing = defaultTiming;
  438. if( slide.hasOwnProperty( 'data-timing' )) {
  439. var t = slide[ 'data-timing' ];
  440. timing = parseInt(t);
  441. if( isNaN(timing) ) {
  442. console.warn("Could not parse timing '" + t + "' of slide " + i + "; using default of " + defaultTiming);
  443. timing = defaultTiming;
  444. }
  445. }
  446. timings.push(timing);
  447. }
  448. callback( timings );
  449. } );
  450. } );
  451. }
  452. /**
  453. * Return the number of seconds allocated for presenting
  454. * all slides up to and including this one.
  455. */
  456. function getTimeAllocated( timings, callback ) {
  457. callRevealApi( 'getSlidePastCount', [], function ( currentSlide ) {
  458. var allocated = 0;
  459. for (var i in timings.slice(0, currentSlide + 1)) {
  460. allocated += timings[i];
  461. }
  462. callback( allocated );
  463. } );
  464. }
  465. /**
  466. * Create the timer and clock and start updating them
  467. * at an interval.
  468. */
  469. function setupTimer() {
  470. var start = new Date(),
  471. timeEl = document.querySelector( '.speaker-controls-time' ),
  472. clockEl = timeEl.querySelector( '.clock-value' ),
  473. hoursEl = timeEl.querySelector( '.hours-value' ),
  474. minutesEl = timeEl.querySelector( '.minutes-value' ),
  475. secondsEl = timeEl.querySelector( '.seconds-value' ),
  476. pacingTitleEl = timeEl.querySelector( '.pacing-title' ),
  477. pacingEl = timeEl.querySelector( '.pacing' ),
  478. pacingHoursEl = pacingEl.querySelector( '.hours-value' ),
  479. pacingMinutesEl = pacingEl.querySelector( '.minutes-value' ),
  480. pacingSecondsEl = pacingEl.querySelector( '.seconds-value' );
  481. var timings = null;
  482. getTimings( function ( _timings ) {
  483. timings = _timings;
  484. if (_timings !== null) {
  485. pacingTitleEl.style.removeProperty('display');
  486. pacingEl.style.removeProperty('display');
  487. }
  488. // Update once directly
  489. _updateTimer();
  490. // Then update every second
  491. setInterval( _updateTimer, 1000 );
  492. } );
  493. function _resetTimer() {
  494. if (timings == null) {
  495. start = new Date();
  496. _updateTimer();
  497. }
  498. else {
  499. // Reset timer to beginning of current slide
  500. getTimeAllocated( timings, function ( slideEndTimingSeconds ) {
  501. var slideEndTiming = slideEndTimingSeconds * 1000;
  502. callRevealApi( 'getSlidePastCount', [], function ( currentSlide ) {
  503. var currentSlideTiming = timings[currentSlide] * 1000;
  504. var previousSlidesTiming = slideEndTiming - currentSlideTiming;
  505. var now = new Date();
  506. start = new Date(now.getTime() - previousSlidesTiming);
  507. _updateTimer();
  508. } );
  509. } );
  510. }
  511. }
  512. timeEl.addEventListener( 'click', function() {
  513. _resetTimer();
  514. return false;
  515. } );
  516. function _displayTime( hrEl, minEl, secEl, time) {
  517. var sign = Math.sign(time) == -1 ? "-" : "";
  518. time = Math.abs(Math.round(time / 1000));
  519. var seconds = time % 60;
  520. var minutes = Math.floor( time / 60 ) % 60 ;
  521. var hours = Math.floor( time / ( 60 * 60 )) ;
  522. hrEl.innerHTML = sign + zeroPadInteger( hours );
  523. if (hours == 0) {
  524. hrEl.classList.add( 'mute' );
  525. }
  526. else {
  527. hrEl.classList.remove( 'mute' );
  528. }
  529. minEl.innerHTML = ':' + zeroPadInteger( minutes );
  530. if (hours == 0 && minutes == 0) {
  531. minEl.classList.add( 'mute' );
  532. }
  533. else {
  534. minEl.classList.remove( 'mute' );
  535. }
  536. secEl.innerHTML = ':' + zeroPadInteger( seconds );
  537. }
  538. function _updateTimer() {
  539. var diff, hours, minutes, seconds,
  540. now = new Date();
  541. diff = now.getTime() - start.getTime();
  542. clockEl.innerHTML = now.toLocaleTimeString( 'en-US', { hour12: true, hour: '2-digit', minute:'2-digit' } );
  543. _displayTime( hoursEl, minutesEl, secondsEl, diff );
  544. if (timings !== null) {
  545. _updatePacing(diff);
  546. }
  547. }
  548. function _updatePacing(diff) {
  549. getTimeAllocated( timings, function ( slideEndTimingSeconds ) {
  550. var slideEndTiming = slideEndTimingSeconds * 1000;
  551. callRevealApi( 'getSlidePastCount', [], function ( currentSlide ) {
  552. var currentSlideTiming = timings[currentSlide] * 1000;
  553. var timeLeftCurrentSlide = slideEndTiming - diff;
  554. if (timeLeftCurrentSlide < 0) {
  555. pacingEl.className = 'pacing behind';
  556. }
  557. else if (timeLeftCurrentSlide < currentSlideTiming) {
  558. pacingEl.className = 'pacing on-track';
  559. }
  560. else {
  561. pacingEl.className = 'pacing ahead';
  562. }
  563. _displayTime( pacingHoursEl, pacingMinutesEl, pacingSecondsEl, timeLeftCurrentSlide );
  564. } );
  565. } );
  566. }
  567. }
  568. /**
  569. * Sets up the speaker view layout and layout selector.
  570. */
  571. function setupLayout() {
  572. layoutDropdown = document.querySelector( '.speaker-layout-dropdown' );
  573. layoutLabel = document.querySelector( '.speaker-layout-label' );
  574. // Render the list of available layouts
  575. for( var id in SPEAKER_LAYOUTS ) {
  576. var option = document.createElement( 'option' );
  577. option.setAttribute( 'value', id );
  578. option.textContent = SPEAKER_LAYOUTS[ id ];
  579. layoutDropdown.appendChild( option );
  580. }
  581. // Monitor the dropdown for changes
  582. layoutDropdown.addEventListener( 'change', function( event ) {
  583. setLayout( layoutDropdown.value );
  584. }, false );
  585. // Restore any currently persisted layout
  586. setLayout( getLayout() );
  587. }
  588. /**
  589. * Sets a new speaker view layout. The layout is persisted
  590. * in local storage.
  591. */
  592. function setLayout( value ) {
  593. var title = SPEAKER_LAYOUTS[ value ];
  594. layoutLabel.innerHTML = 'Layout' + ( title ? ( ': ' + title ) : '' );
  595. layoutDropdown.value = value;
  596. document.body.setAttribute( 'data-speaker-layout', value );
  597. // Persist locally
  598. if( supportsLocalStorage() ) {
  599. window.localStorage.setItem( 'reveal-speaker-layout', value );
  600. }
  601. }
  602. /**
  603. * Returns the ID of the most recently set speaker layout
  604. * or our default layout if none has been set.
  605. */
  606. function getLayout() {
  607. if( supportsLocalStorage() ) {
  608. var layout = window.localStorage.getItem( 'reveal-speaker-layout' );
  609. if( layout ) {
  610. return layout;
  611. }
  612. }
  613. // Default to the first record in the layouts hash
  614. for( var id in SPEAKER_LAYOUTS ) {
  615. return id;
  616. }
  617. }
  618. function supportsLocalStorage() {
  619. try {
  620. localStorage.setItem('test', 'test');
  621. localStorage.removeItem('test');
  622. return true;
  623. }
  624. catch( e ) {
  625. return false;
  626. }
  627. }
  628. function zeroPadInteger( num ) {
  629. var str = '00' + parseInt( num );
  630. return str.substring( str.length - 2 );
  631. }
  632. /**
  633. * Limits the frequency at which a function can be called.
  634. */
  635. function debounce( fn, ms ) {
  636. var lastTime = 0,
  637. timeout;
  638. return function() {
  639. var args = arguments;
  640. var context = this;
  641. clearTimeout( timeout );
  642. var timeSinceLastCall = Date.now() - lastTime;
  643. if( timeSinceLastCall > ms ) {
  644. fn.apply( context, args );
  645. lastTime = Date.now();
  646. }
  647. else {
  648. timeout = setTimeout( function() {
  649. fn.apply( context, args );
  650. lastTime = Date.now();
  651. }, ms - timeSinceLastCall );
  652. }
  653. }
  654. }
  655. })();
  656. </script>
  657. </body>
  658. </html>