Understanding JS Event Loop

Developer
Size
2,268 Kb
Views
6,072

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 Previews

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);
});
Understanding JS Event Loop - Script Codes
Understanding JS Event Loop - Script Codes
Home Page Home
Developer Jek Bao Choo
Username jek
Uploaded January 19, 2023
Rating 3
Size 2,268 Kb
Views 6,072
Do you need developer help for Understanding JS Event Loop?

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!

Jek Bao Choo (jek) Script Codes
Create amazing love letters with AI!

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!