- 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.
- Spring Cloud Alibaba核心技術與實戰案例
- 新手學Visual C# 2008程序設計
- C語言程序設計案例式教程
- Machine Learning in Java
- C專家編程
- 區塊鏈技術進階與實戰(第2版)
- UI設計全書(全彩)
- Learning Modular Java Programming
- MySQL 8從零開始學(視頻教學版)
- Learning D
- Drupal 8 Development Cookbook(Second Edition)
- 數據結構:Python語言描述
- Spark技術內幕:深入解析Spark內核架構設計與實現原理
- HTML5/CSS3/JavaScript技術大全
- 信息學競賽寶典:基礎算法