- HTML5 for Flash Developers
- Matt Fisher
- 205字
- 2021-08-13 16:28:27
Timing JavaScript execution
One important point to note while dealing with JavaScript execution timing is to be sure that the entire page has finished loading prior to allowing the JavaScript to begin executing its code. The reason for waiting for page to load is to allow all of the assets and external references to load on the page before attempting to manipulate them. If your JavaScript attempts to perform an action on an element that doesn't exist, your application flow could fail. To avoid this issue, we can add an event listener to the DOM allowing it to run only once the page has been completely loaded and comes to display. Utilizing the DOM event for JavaScript offers an easy way to do just this as shown in the following code:
window.addEventListener("load", init, false); var init = function() { // Start everything from in here. }
Now, when the window has completed its load process the init
function is called and the remainder of the application code can begin execution. JavaScript actually has a number of ways to accomplish execution of the code, specifically once the page has finished loading. The following chapters of this book will use and explain many of them using examples.
- Android Wearable Programming
- CentOS 7 Linux Server Cookbook(Second Edition)
- SQL語言從入門到精通
- Python Network Programming Cookbook(Second Edition)
- 網絡爬蟲原理與實踐:基于C#語言
- PySpark Cookbook
- Visual C++開發入行真功夫
- Lighttpd源碼分析
- Python算法詳解
- 學習OpenCV 4:基于Python的算法實戰
- Python機器學習算法與應用
- 奔跑吧 Linux內核
- Flink核心技術:源碼剖析與特性開發
- Getting Started with JUCE
- Java服務端研發知識圖譜