衚珟力豊かなJavaScriptむベントの凊理

内容




あなたはあなたの心に力を持っおいたすが、倖郚の出来事には力がありたせん。 これを理解するず、あなたは力を埗たす。
マヌカスアりレリりス、瞑想。

䞀郚のプログラムは、ナヌザヌ入力、マりス、およびキヌボヌドで動䜜したす。 そのような入力の発生時刻ずデヌタのシヌケンスは、事前に予枬するこずはできたせん。 これには、プログラムの実行順序を制埡するために、すでによく知られおいるものずは異なるアプロヌチが必芁です。

むベントハンドラヌ


キヌボヌドのボタンが抌されたかどうかを確認する唯䞀の方法が、ボタンの珟圚の状態を読み取るこずであるむンタヌフェヌスを想像しおください。 クリックに応答するには、ボタンの状態を垞に読み取っお、ボタンが攟されるたでこの状態をキャッチできるようにする必芁がありたす。 抌す瞬間をスキップできるため、プロセッサ時間を消費する他の蚈算を行うこずは危険です。

したがっお、入力はプリミティブデバむスで凊理されたした。 ハヌドりェアたたはOSがボタンが抌されたこずを認識し、それをキュヌに転送した堎合、前進したす。 その埌、プログラムは定期的に新しいむベントのキュヌをチェックし、キュヌにあるものに応答できたす。

もちろん、ボタンを抌しおからプログラムがそれに気付いお反応するたでの時間が長いず、このプログラムがゆっくり実行されおいるず認識されるため、圌女はチェックするこずを忘れないでください。 このアプロヌチはめったに䜿甚されたせん。

より良いオプションは、むベントが発生したずきにコヌドがむベントに応答できるようにする䞀皮の䞭間システムです。 ブラりザヌでは、特定のむベントのハンドラヌずしお関数を登録するこずにより、これを行うこずができたす。

<p>     .</p> <script> addEventListener("click", function() { console.log("!"); }); </script> 


addEventListener関数は、2番目の匕数を、最初の匕数で説明されおいるむベントが発生したずきに呌び出される関数ずしお登録したす。

むベントずDOMノヌド


各ブラりザヌむベントハンドラヌはコンテキストで登録されたす。 ブラりザではグロヌバルスコヌプがりィンドりオブゞェクトであるため、addEventListenerを呌び出すずきは、りィンドり党䜓のメ゜ッドずしお呌び出したす。 各DOM芁玠には独自のaddEventListenerメ゜ッドがあり、この芁玠からむベントをリッスンできたす。

 <button>  .</button> <p>   .</p> <script> var button = document.querySelector("button"); button.addEventListener("click", function() { console.log(" ."); }); </script> 


この䟋では、ボタンのDOMノヌドにハンドラヌを割り圓おたす。 ボタンを抌すずハンドラヌが起動し、ドキュメントの他の郚分をクリックしおも起動したせん。

onclick属性をノヌドに割り圓おるこずも同様に機胜したす。 ただし、ノヌドにはonclick属性が1぀しかありたせん。぀たり、この方法では1぀のハンドラヌしか登録できたせん。 addEventListenerメ゜ッドを䜿甚するず、任意の数のハンドラヌを远加できるため、以前にランダムに割り圓おられたハンドラヌを眮き換えるこずはありたせん。

addEventListenerず同じ匕数で呌び出されたremoveEventListenerメ゜ッドは、ハンドラヌを削陀したす。

 <button>Act-once button</button> <script> var button = document.querySelector("button"); function once() { console.log("Done."); button.removeEventListener("click", once); } button.addEventListener("click", once); </script> 


これを行うには、関数に名前を付けこの堎合は1回、addEventListenerずremoveEventListenerの䞡方に枡すこずができるようにしたす。

むベントオブゞェクト


䟋では、匕数がハンドラヌ関数むベントオブゞェクトに枡されるずいう事実を無芖したした。 むベントに関する远加情報を保存したす。 たずえば、クリックされたマりスボタンを知る必芁がある堎合、このオブゞェクトのどのプロパティを参照できたす。

 <button> ,  !</button> <script> var button = document.querySelector("button"); button.addEventListener("mousedown", function(event) { if (event.which == 1) console.log(""); else if (event.which == 2) console.log(""); else if (event.which == 3) console.log(""); }); </script> 


オブゞェクトに保存される情報は、むベントの皮類ごずに異なりたす。 これらのタむプに぀いおは埌で説明したす。 タむプオブゞェクトのプロパティには、垞にむベントを説明する文字列が含たれたすたずえば、「クリック」たたは「マりス」。

䌝播


子ノヌドを持぀ノヌドに登録されたむベントは、子に発生したむベントを受け取りたす。 段萜内のボタンをクリックするず、段萜のむベントハンドラヌがクリックむベントを受け取りたす。

段萜ずボタンの䞡方にハンドラヌがある堎合、より具䜓的なものが最初に起動したす。぀たり、ボタンハンドラヌです。 むベントは、それが発生したノヌドからその芪、さらにドキュメントのルヌトたで倖偎に広がりたす。 すべおの䞭間ノヌドのすべおのハンドラヌを凊理した埌、むベントに応答するキュヌはりィンドり自䜓に到達したす。

ハンドラはい぀でも、むベントオブゞェクトのstopPropagationメ゜ッドを呌び出しお、「䞊䜍」ノヌドがそれを受け取らないようにするこずができたす。 これは、別のクリック可胜な芁玠内にボタンがあり、ボタンをクリックしお倖郚芁玠の動䜜をアクティブにしたくない堎合に圹立ちたす。

次の䟋では、ボタンず呚囲の段萜の䞡方に「mousedown」ハンドラヌを登録したす。 右クリックするず、ボタンハンドラヌはstopPropagationを呌び出し、段萜ハンドラヌが開始されないようにしたす。 別のボタンをクリックするず、䞡方のハンドラヌが起動したす。

 <p>  <button> </button>.</p> <script> var para = document.querySelector("p"); var button = document.querySelector("button"); para.addEventListener("mousedown", function() { console.log(" ."); }); button.addEventListener("mousedown", function(event) { console.log(" ."); if (event.which == 3) event.stopPropagation(); }); </script> 


ほずんどのむベントオブゞェクトには、凊理を開始したノヌドを参照するタヌゲットプロパティがありたす。 䞍芁なノヌドからの䜕かを凊理しおいないこずを確認するために䜿甚できたす。

タヌゲットプロパティを䜿甚しお、特定の皮類のむベントの凊理を拡匵するこずもできたす。 たずえば、ボタンの長いリストを含むノヌドがある堎合、ノヌドのむベントハンドラヌを1぀登録し、各ボタンのハンドラヌを個別に登録するのではなく、ボタンがクリックされたかどうかを調べる方が䟿利です。

 <button>A</button> <button>B</button> <button>C</button> <script> document.body.addEventListener("click", function(event) { if (event.target.nodeName == "BUTTON") console.log("Clicked", event.target.textContent); }); </script> 


デフォルトのアクション


倚くのむベントにはデフォルトのアクションがありたす。 リンクをクリックするず、リンクをたどりたす。 䞋矢印をクリックするず、ブラりザはペヌゞを䞋にスクロヌルしたす。 右クリックするず、コンテキストメニュヌが衚瀺されたす。 などなど。

ほずんどのタむプのむベントでは、デフォルトアクションが起動する前にむベントハンドラヌが呌び出されたす。 ハンドラヌがこのアクションを発生させたくない堎合倚くの堎合、既にアクションを凊理しおいるため、むベントオブゞェクトのpreventDefaultメ゜ッドを呌び出すこずができたす。

これを䜿甚しお、独自のキヌボヌドショヌトカットたたはコンテキストメニュヌを䜜成できたす。 たた、䜿い慣れたナヌザヌむンタヌフェむスを壊すためにも䜿甚できたす。 たずえば、ここにあなたがたどるこずができないリンクがありたす。

 <a href="https://developer.mozilla.org/">MDN</a> <script> var link = document.querySelector("a"); link.addEventListener("click", function(event) { console.log("."); event.preventDefault(); }); </script> 


これをしないでください-非垞に深刻な理由がない限り ペヌゞのナヌザヌは、アクションの予期しない結果に遭遇するず非垞に䞍快になりたす。 ブラりザによっおは、䞀郚のむベントをむンタヌセプトできたせん。 Chromeでは、珟圚のブックマヌクを閉じるためのホットキヌCtrl-WたたはCommand-Wを凊理できたせん。

キヌボヌドボタンからのむベント


キヌボヌドのボタンが抌されるず、ブラりザヌはkeydownむベントを発生させたす。 リリヌスされるず、キヌアップむベントが発生したす。

 <p>   V .</p> <script> addEventListener("keydown", function(event) { if (event.keyCode == 86) document.body.style.background = "violet"; }); addEventListener("keyup", function(event) { if (event.keyCode == 86) document.body.style.background = ""; }); </script> 


名前にもかかわらず、「キヌダりン」はボタンが抌されたずきだけでなく発生したす。 ボタンを抌したたたにするず、キヌからの2番目の信号が到着するたびにむベントが発生したすキヌリピヌト。 たずえば、矢印付きのボタンを抌したずきにゲヌムキャラクタヌの速床を䞊げ、ボタンを攟したずきに速床を䞋げる必芁がある堎合、ボタンからの信号を繰り返すたびに速床を䞊げないように泚意する必芁がありたす。そうしないず、速床が非垞に速くなりたす。

この䟋では、むベントオブゞェクトのkeyCodeプロパティに蚀及しおいたす。 したがっお、どのボタンが抌されたか、離されたかを調べるこずができたす。 残念ながら、数倀コヌドを目的のボタンに倉換する方法は必ずしも明らかではありたせん。

数字ず文字の堎合、コヌドはボタンに衚瀺される倧文字に関連付けられたUnicode文字コヌドになりたす。 文字列メ゜ッドcharCodeAtはこのコヌドを提䟛したす。

 console.log("Violet".charCodeAt(0)); // → 86 console.log("1".charCodeAt(0)); // → 49 


他のボタンには予枬䞍可胜なコヌドがありたす。 それらを芋぀ける最良の方法は実隓的です。 キヌコヌドを曞き蟌むハンドラヌを登録し、目的のボタンを抌したす。

Shift、Ctrl、Alt、MetaMacのコマンドなどの修食ボタンは、通垞のボタンず同様にむベントを䜜成したす。 ただし、キヌボヌドショヌトカットを解析する堎合、キヌボヌドむベントおよびマりスむベントのshiftKey、ctrlKey、altKey、およびmetaKeyプロパティを介しお修食子が抌されたかどうかを確認できたす。

 <p> Ctrl-Space  .</p> <script> addEventListener("keydown", function(event) { if (event.keyCode == 32 && event.ctrlKey) console.log("!"); }); </script> 


「キヌダりン」および「キヌアップ」むベントは、ボタンを物理的に抌すこずに関する情報を提䟛したす。 たた、ナヌザヌが入力するテキストを知る必芁がある堎合はどうなりたすか ボタンを抌しお䜜成するのは䞍䟿です。 これを行うために、「keydown」の盎埌に発生する「keypress」むベントがありたすキヌがただ保持されおいる堎合は「keydown」ずずもに繰り返されたすが、文字を発行するボタンのみです。 charCodeむベントオブゞェクトプロパティには、Unicodeコヌドずしお解釈できるコヌドが含たれおいたす。 String.fromCharCode関数を䜿甚しお、コヌドを単䞀の文字列に倉換できたす。

 <p>     .</p> <script> addEventListener("keypress", function(event) { console.log(String.fromCharCode(event.charCode)); }); </script> 


ノヌドは、キヌストロヌク䞭に入力フォヌカスがあった堎所に応じお、キヌストロヌクむベントの゜ヌスになりたす。 通垞のノヌドはtabindex属性を蚭定しない限り入力フォヌカスを取埗できたせんが、リンク、ボタン、フォヌムフィヌルドなどは取埗できたす。 第18章の入力フィヌルドに戻りたす。フォヌカスがない堎合、document.bodyはタヌゲットむベントノヌドずしお機胜したす。

マりスボタン


マりスをクリックするず、いく぀かのむベントもトリガヌされたす。 mousedownむベントずmouseupむベントはkeydownむベントずkeyupむベントに䌌おおり、ボタンが抌されたずきず離されたずきに発生したす。 むベントは、マりスカヌ゜ルが眮かれたDOMノヌドで発生したす。

「マりスアップ」むベントの埌、ヒットしおリリヌスされたノヌドで「クリック」むベントがトリガヌされたす。 たずえば、ある段萜䞊のボタンをクリックしおから、マりスを別の段萜に移動しおボタンを離すず、これらの䞡方の段萜を含む芁玠で「クリック」むベントが発生したす。

2回のクリックが次々ず発生する堎合、2回目の「クリック」の実行盎埌に「dblclick」ダブルクリックむベントが発生したす。

マりスむベントが発生した堎所の正確な座暙を取埗するには、pageXプロパティずpageYプロパティを参照したす。これらのプロパティには、巊䞊隅を基準ずしたピクセル単䜍の座暙が含たれたす。

この䟋では、プリミティブな描画プログラムが䜜成されたす。 ドキュメントをクリックするたびに、カヌ゜ルの䞋にポむントが远加されたす。 第19章では、あたり原始的でない描画プログラムを玹介したす。

 <style> body { height: 200px; background: beige; } .dot { height: 8px; width: 8px; border-radius: 4px; /*   */ background: blue; position: absolute; } </style> <script> addEventListener("click", function(event) { var dot = document.createElement("div"); dot.className = "dot"; dot.style.left = (event.pageX - 4) + "px"; dot.style.top = (event.pageY - 4) + "px"; document.body.appendChild(dot); }); </script> 


clientXおよびclientYプロパティはpageXおよびpageYに䌌おいたすが、珟圚衚瀺されおいるドキュメントの郚分に盞察的な座暙を提䟛したすドキュメントがスクロヌルされおいる堎合。 これは、マりスの座暙をgetBoundingClientRectが返す座暙ず比范するずきに䟿利です。その戻り倀は、ドキュメントの衚瀺郚分の盞察座暙にも関連しおいたす。

マりスの動き


マりスカヌ゜ルを移動するたびに、mousemoveむベントが発生したす。 マりスの䜍眮を远跡するために䜿甚できたす。 通垞、これはマりスでオブゞェクトをドラッグするこずに関連する機胜を䜜成するずきに必芁です。

たずえば、次のプログラムはバヌを衚瀺し、巊右に移動するず幅が増枛するようにむベント凊理を蚭定したす。

 <p>    :</p> <div style="background: orange; width: 60px; height: 20px"> </div> <script> var lastX; //    var rect = document.querySelector("div"); rect.addEventListener("mousedown", function(event) { if (event.which == 1) { lastX = event.pageX; addEventListener("mousemove", moved); event.preventDefault(); //   } }); function moved(event) { if (event.which != 1) { removeEventListener("mousemove", moved); } else { var dist = event.pageX - lastX; var newWidth = Math.max(10, rect.offsetWidth + dist); rect.style.width = newWidth + "px"; lastX = event.pageX; } } </script> 


「mousemove」ハンドラヌはりィンドり党䜓に登録されるこずに泚意しおください。 マりスがストリップを超えた堎合でも、ボタンを攟したずきにサむズを曎新しお停止する必芁がありたす。

カヌ゜ルがノヌドに出入りするず、「マりスオヌバヌ」たたは「マりスアりト」むベントが発生したす。 特に、カヌ゜ルをこの芁玠の䞊に眮いたずきにマりスを保持したり、䜕かのスタむルを衚瀺たたは倉曎したりする効果を䜜成するために䜿甚できたす。

残念ながら、このような効果の䜜成は、mouseoverむベントでトリガヌし、mouseoutむベントで終了するこずに限定されたせん。 マりスがノヌドからその子ノヌドに移動するず、䞀般的に蚀えば、マりスはそれを離れたせんでしたが、マりスアりトむベントが芪ノヌドで発生したす。 さらに悪いこずに、これらのむベントは他のすべおのむベントず同じように䌝播するため、ハンドラヌを登録したノヌドの子の1぀からカヌ゜ルが離れるず、「マりスアりト」が発生したす。

この問題を回避するには、むベントオブゞェクトのrelatedTargetプロパティを䜿甚したす。 mouseoverむベントが発生したずきにマりスがどのノヌドの前にあったか、mouseoutむベントが発生したずきにどの芁玠にゞャンプするかを瀺したす。 relatedTargetがタヌゲットノヌドの倖偎にある堎合にのみ、゚フェクトを倉曎する必芁がありたす。 この堎合にのみ、むベントは実際にノヌドぞの遷移たたはノヌドからの離脱を衚したす。

 <p>    <strong> </strong>.</p> <script> var para = document.querySelector("p"); function isInside(node, target) { for (; node != null; node = node.parentNode) if (node == target) return true; } para.addEventListener("mouseover", function(event) { if (!isInside(event.relatedTarget, para)) para.style.color = "red"; }); para.addEventListener("mouseout", function(event) { if (!isInside(event.relatedTarget, para)) para.style.color = ""; }); </script> 


isInside関数は、ドキュメントの最䞊郚に到達するたでそしおノヌドがnullになるたでノヌドのすべおの祖先を反埩するか、指定された芪を芋぀けられたせん。

以䞋に瀺すように、ホバヌず呌ばれるCSS擬䌌セレクタヌを䜿甚するず、このような効果をはるかに簡単に達成できるこずを远加する必芁がありたす。 ただし、ホバヌ時にノヌドのスタむルを倉曎するよりも耇雑な操䜜が必芁な堎合は、mouseoverむベントずmouseoutむベントでトリックを䜿甚する必芁がありたす。

 <style> p:hover { color: red } </style> <p>    <strong> </strong>.</p> 


スクロヌルむベント



アむテムがスクロヌルするず、「スクロヌル」むベントが発生したす。 これは倚くの堎合に䜿甚されたす。たずえば、ナヌザヌが芋おいるものを芋぀ける画面に衚瀺されないアニメヌションを停止する、たたは悪意のある本郚に秘密のスパむレポヌトを送信する、たたは進行状況を芖芚的に瀺すコンテンツの䞀郚を匷調衚瀺する、ペヌゞ番号を衚瀺するために䜿甚されたす。

この䟋では、ドキュメントの右䞊隅にプロセスむンゞケヌタが䜜成され、アむテムを䞋にスクロヌルするず塗り぀ぶされたす。

 <style> .progress { border: 1px solid blue; width: 100px; position: fixed; top: 10px; right: 10px; } .progress > div { height: 12px; background: blue; width: 0%; } body { height: 2000px; } </style> <div class="progress"><div></div></div> <p>Scroll me...</p> <script> var bar = document.querySelector(".progress div"); addEventListener("scroll", function() { var max = document.body.scrollHeight - innerHeight; var percent = (pageYOffset / max) * 100; bar.style.width = percent + "%"; }); </script> 


固定芁玠の䜍眮は、絶察芁玠ずほが同じこずを意味したすが、ドキュメントの残りの郚分ずずもに芁玠がスクロヌルするのを防ぎたす。 ポむントは、むンゞケヌタヌを隅に残すこずです。 内郚には、珟圚の進行状況を反映するためにサむズ倉曎される別の芁玠がありたす。 芁玠のサむズがむンゞケヌタヌ党䜓のサむズに察しお倉化するように、pxではなくパヌセンテヌゞを䜿甚しお幅を蚭定したす。

グロヌバル倉数innerHeightはりィンドりの高さを指定したす。これは、スクロヌル可胜な芁玠の党高から差し匕く必芁がありたす。芁玠の最埌に到達するず、スクロヌルが終了したす。 innerHeightに加えお、倉数innerWidthもありたす。 pageYOffsetの珟圚のスクロヌル䜍眮を最倧スクロヌル䜍眮で陀算し、100倍するず、むンゞケヌタヌのパヌセンテヌゞが埗られたす。

preventDefaultを呌び出しおも、スクロヌルは防止されたせん。 むベントハンドラは、スクロヌルが発生した埌に呌び出されたす。

フォヌカスむベント


芁玠がフォヌカスを受け取るず、ブラりザは「focus」むベントを発生させたす。 フォヌカスを倱うず、「がかし」むベントが発生したす。

以前のむベントずは異なり、これら2぀は察象倖です。 芪ノヌドのハンドラヌは、子によるフォヌカスの受信たたは喪倱に぀いお通知されたせん。

次の䟋は、珟圚フォヌカスがあるテキストフィヌルドのツヌルチップテキストを瀺しおいたす。

 <p>: <input type="text" data-help="  "></p> <p>: <input type="text" data-help="  "></p> <p id="help"></p> <script> var help = document.querySelector("#help"); var fields = document.querySelectorAll("input"); for (var i = 0; i < fields.length; i++) { fields[i].addEventListener("focus", function(event) { var text = event.target.getAttribute("data-help"); help.textContent = text; }); fields[i].addEventListener("blur", function(event) { help.textContent = ""; }); } </script> 


ナヌザヌがドキュメントを衚瀺するブラりザヌのブックマヌクたたはブラりザヌりィンドりからフォヌカスを遞択たたは削陀するず、りィンドりオブゞェクトはフォヌカスむベントずブラヌむベントを受け取りたす。

アップロヌドむベント


ペヌゞの読み蟌みが完了するず、りィンドりオブゞェクトず本文オブゞェクトで「load」むベントが発生したす。 これは、完党に構築されたドキュメントを必芁ずする初期化アクティビティを蚈画するためによく䜿甚されたす。 タグの内容を思い出しおください
   ,    .     – ,    -     ,     . 

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });

, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>

, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });

, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>

, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
   ,    .     – ,    -     ,     . 

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>

, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>

, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
   ,    .     – ,    -     ,     . 

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
   ,    .     – ,    -     ,     . 

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
   ,    .     – ,    -     ,     . 

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>

, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>

, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
   ,    .     – ,    -     ,     . 

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
   ,    .     – ,    -     ,     . 

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>

, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>

, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>

, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>

, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
   ,    .     – ,    -     ,     . 

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
   ,    .     – ,    -     ,     . 

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
, . – , - , .

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>
   ,    .     – ,    -     ,     . 

, , “load”, , . , .

(, ), "beforeunload". – . , , preventDefault. . , , . , , , , .


. - . – . 13 requestAnimationFrame, . .

, , . , . , . , , .

. , . , .

, JavaScript , . - , , « » (web worker) – JavaScript, , .

, code/squareworker.js:
addEventListener("message", function(event) { postMessage(event.data * event.data); });


, – , , . «», , .

var squareWorker = new Worker("code/squareworker.js"); squareWorker.addEventListener("message", function(event) { console.log("The worker responded:", event.data); }); squareWorker.postMessage(10); squareWorker.postMessage(24);

postMessage , “message” . , , Worker, , , – , .



setTimeout requestAnimationFrame. . , . :

<script> document.body.style.background = "blue"; setTimeout(function() { document.body.style.background = "yellow"; }, 2000); </script>

. , , setTimeout, clearTimeout.

var bombTimer = setTimeout(function() { console.log("BOOM!"); }, 500); if (Math.random() < 0.5) { // 50% chance console.log("Defused."); clearTimeout(bombTimer); }

cancelAnimationFrame , clearTimeout – , requestAnimationFrame, ( ).

, setInterval clearInterval , X .

var ticks = 0; var clock = setInterval(function() { console.log("tick", ticks++); if (ticks == 10) { clearInterval(clock); console.log("stop."); } }, 200);

(debouncing)
(, "mousemove" "scroll"). «», , .

- , setTimeout, , . « » . .

-, , . , , . , . , , (, ), .

<textarea> -...</textarea> <script> var textarea = document.querySelector("textarea"); var timeout; textarea.addEventListener("keydown", function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log(" ."); }, 500); }); </script>

undefined clearTimeout, , , . , , .

, , , , . , "mousemove", , 250 .

<script> function displayCoords(event) { document.body.textContent = " " + event.pageX + ", " + event.pageY; } var scheduled = false, lastEvent; addEventListener("mousemove", function(event) { lastEvent = event; if (!scheduled) { scheduled = true; setTimeout(function() { scheduled = false; displayCoords(lastEvent); }, 250); } }); </script>


, . addEventListener.

("keydown", "focus", ). DOM, , .

. , (stopPropagation) (preventDefault).

"keydown", "keypress" "keyup". "mousedown", "mouseup" "click". "mousemove", "mouseenter" "mouseout".

“scroll”, "focus" "blur". , window “load”.

. .



1928 2013 Q, W X . – , .

, , . .

<input type="text"> <script> var field = document.querySelector("input"); // Your code here. </script>


JavaScript, , . « » - , .

, . , . .

. , . – , , "mousemove".

<style> .trail { /* className , */ position: absolute; height: 6px; width: 6px; border-radius: 3px; background: teal; } body { height: 300px; } </style> <script> // . </script>


. , .

. asTabs, DOM, , . , , , data-tabname. , , ( display style none), .

, , .

<div id="wrapper"> <div data-tabname="one"> </div> <div data-tabname="two"> </div> <div data-tabname="three"> </div> </div> <script> function asTabs(node) { // . } asTabs(document.querySelector("#wrapper")); </script>

Source: https://habr.com/ru/post/J244041/


All Articles