Understanding JS Event Loop
How do I make an understanding js event loop?
What is a understanding js event loop? How do you make a understanding js event loop? This script and codes were developed by Jek Bao Choo on 19 January 2023, Thursday.
Understanding JS Event Loop - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Understanding JS Event Loop</title>
</head>
<body> <!-- load angular via CDN -->
<script src="//code.angularjs.org/1.4.3/angular.min.js"></script>
Understanding JavaScript event loop
<!--- Part 1 -->
<div class="container"> Part 1 - Native JS event listener <input id="name" />
</div>
<!-- Part 2 -->
<div ng-app="myApp"> <div class="container" ng-controller="MainController"> Part 2 - AngularJS event loop, watcher & digest loop <input ng-model="handle" /> {{ handle }} </div> </div <script src="js/index.js"></script>
</body>
</html>
Understanding JS Event Loop - Script Codes JS Codes
/* * JavaScript event loop responds to * example keypress, click, mouseover, change etc... */
/*
Part 1 to Understanding JavaScript Event Loop
*/
var udsEventLoop = document.getElementById("name");
console.log(udsEventLoop);
function logIt() { console.log("Pressed!"); alert("Oh Pressed!"); /*Commented out to make sure it's not too annoying */
}
udsEventLoop.addEventListener("keypress", logIt);
/* Part 2 Understanding Event Loop with AngularJS */
var myApp = angular.module('myApp', []);
myApp.controller('MainController', function($scope) { $scope.handle = ''; function forWatcher(oldValue, newValue) { console.info('oldValue: ' + oldValue); console.log('newValue: ' + newValue); } $scope.$watch('handle', forWatcher); function changeHandleValue() { $scope.handle = 'whatIsMyHandle'; console.info('Changed handle to ' + $scope.handle); } /* * this won't change the value in the form. It's because it's in a separate event loop to * that of AngularJS created. By doing so it creates 2 event loops. One by angular. The other by my native JS below * If it's in a separate event loop, AngularJS won't watch for it and won't run it through the digest loop * Therefore in order for it to go to the AngularJS event loop we need to use $scope.$apply */ setTimeout(changeHandleValue, 3000); function changeHandleValueScopeApply() { $scope.$apply(function() { $scope.handle = 'whatIsMyHandle2'; console.info('Changed handle to ' + $scope.handle); }); } /* Hence by using apply we can see the entire scope change with the setTimeout. it's becuase the entire digest cycle runs too */ setTimeout(changeHandleValueScopeApply, 6000);
});
Developer | Jek Bao Choo |
Username | jek |
Uploaded | January 19, 2023 |
Rating | 3 |
Size | 2,268 Kb |
Views | 6,072 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Parallax Scrolling Website | 1,603 Kb |
Using Bootstrap | 3,518 Kb |
Using TauCharts | 2,315 Kb |
Working with JS Functions | 3,110 Kb |
Using Google Chart | 2,144 Kb |
Imagining our data import steps | 3,095 Kb |
Playing with Bootstrap Column Ordering | 1,564 Kb |
Rosen Center Nav | 3,677 Kb |
Playing with Typeahead.js | 2,109 Kb |
Working with JavaScript Arrays and Dictionaries | 2,775 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Learning canvas drawing | Aurer | 2,204 Kb |
Print element on a page | Mrs_snow | 2,081 Kb |
Subscription Newsletter - Before inlining | Bradstrong | 7,526 Kb |
Draggables in pure angular | Rlo206 | 5,167 Kb |
Awesome | Samarthpd | 2,901 Kb |
A Pen by Markku Lehmonen | SharpDal | 7,804 Kb |
ABVI Menu Discarded | Overdrivemachines | 3,607 Kb |
The CodePen Logo | Kindofone | 4,259 Kb |
CSS3 Fullscreen Background Slideshow | Leetech | 3,435 Kb |
Pattern lab logo | TimPietrusky | 3,666 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!