DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

The Latest Coding Topics

article thumbnail
How JavaScript Timers Work
At a fundamental level it's important to understand how JavaScript timers work. Oftentimes they behave unintuitively because of the single thread which they are in. Let's start by examining the three functions that we have access to with which to construct and manipulate timers. var id = setTimeout(fn, delay); - Initiates a single timer which will call the specified function after the delay. The function returns a unique ID with which the timer can be canceled at a later time. var id = setInterval(fn, delay); - Similar to setTimeout but continually calls the function (with a delay every time) until it is canceled. clearInterval(id); - Accepts a timer ID (returned by either of the aforementioned functions) and stops the timer callback from occurring. In order to understand how the timers work internally there's one important concept that needs to be explored: timer delay is not guaranteed. Since all JavaScript in a browser executes on a single thread, asynchronous events (such as mouse clicks and timers) are only run when there's been an opening in the execution. This is best demonstrated with a diagram, like in the following: There are Plenty of Reasons Why JavaScript Timers Matter You may become frustrated if you just look at JavaScript timers as something that doesn’t serve any real purpose for you. It is easy to get yourself into that mentality, but it is also not entirely productive or accurate to think of them in this way. The truth is that JavaScript timers are extremely effective, and they help control various processes that need to be controlled. You may want to set up timers to go off when someone clicks their mouse or when a certain amount of time has passed. Whatever the case may be, you need those timers to help you take care of what is happening. JavaScript timers have already proven themselves to be incredibly useful for many people, and they can be for you as well. Since JavaScript can only ever execute one piece of code at a time (due to its single-threaded nature) each of these blocks of code is "blocking" the progress of other asynchronous events. This means that when an asynchronous event occurs (like a mouse click, a timer firing, or an XMLHttpRequest completing) it gets queued up to be executed later (how this queueing actually occurs surely varies from browser to browser, so consider this to be a simplification). To start with, within the first block of JavaScript, two timers are initiated: a 10ms setTimeout and a 10ms setInterval. Due to where and when the timer was started it actually fires before we actually complete the first block of code. Note, however, that it does not execute immediately (it is incapable of doing that, because of the threading). Instead, that delayed function is queued in order to be executed at the next available moment. Additionally, within this first JavaScript block, we see a mouse click occur. The JavaScript callbacks associated with this asynchronous event (we never know when a user may perform an action, thus it's considered to be asynchronous) are unable to be executed immediately thus, like the initial timer, it is queued to be executed later. After the initial block of JavaScript finishes executing the browser immediately asks the question: What is waiting to be executed? In this case, both a mouse click handler and a timer callback are waiting. The browser then picks one (the mouse click callback) and executes it immediately. The timer will wait until the next possible time, in order to execute. Note that while the mouse click handler is executing the first interval callback executes. As with the timer, its handler is queued for later execution. However, note that when the interval is fired again (when the timer handler is executing) this time that handler execution is dropped. If you were to queue up all interval callbacks when a large block of code is executing the result would be a bunch of intervals executing with no delay between them, upon completion. Instead, browsers tend to simply wait until no more interval handlers are queued (for the interval in question) before queuing more. We can, in fact, see that this is the case when a third interval callback fires while the interval, itself, is executing. This shows us an important fact: Intervals don't care about what is currently executing, they will queue indiscriminately, even if it means that the time between callbacks will be sacrificed. Finally, after the second interval callback is finished executing, we can see that there's nothing left for the JavaScript engine to execute. This means that the browser now waits for a new asynchronous event to occur. We get this at the 50ms mark when the interval fires again. This time, however, there is nothing blocking its execution, so it fires immediately. Let's take a look at an example to better illustrate the differences between setTimeout and setInterval. setTimeout(function(){ /* Some long block of code... */ setTimeout(arguments.callee, 10); }, 10); setInterval(function(){ /* Some long block of code... */ }, 10); These two pieces of code may appear to be functionally equivalent, at first glance, but they are not. Notably, the setTimeout code will always have at least a 10ms delay after the previous callback execution (it may end up being more, but never less) whereas the setInterval will attempt to execute a callback every 10ms regardless of when the last callback was executed. There's a lot that we've learned here, let's recap: JavaScript engines only have a single thread, forcing asynchronous events to queue waiting for execution. setTimeout and setInterval are fundamentally different in how they execute asynchronous code. If a timer is blocked from immediately executing it will be delayed until the next possible point of execution (which will be longer than the desired delay). Intervals may execute back-to-back with no delay if they take long enough to execute (longer than the specified delay). All of this is incredibly important knowledge to build off of. Knowing how a JavaScript engine works, especially with the large number of asynchronous events that typically occur, makes for a great foundation when building an advanced piece of application code. This is an excerpt from my work-in-progress book: Secrets of the JavaScript Ninja. To be released Fall 2008. Previous excerpt: Partial Function Application in JavaScript
May 22, 2023
by John Resig
· 20,847 Views · 1 Like
article thumbnail
How it Feels to Switch from Eclipse to Android Studio
So, Android Studio exists. While there are a number of fixes for the less-than-graceful aspects of Android development in Eclipse - Genymotion, right? - some are moving to Android Studio for a more stream-lined approach. This recent post from MeetMe's engineering blog details Bill Donahue's switch from Eclipse to Android Studio, and he has some pretty strong feelings about it. He says - and this is his own emphasis - the following: I will never go back to Eclipse Donahue then explains the key differences as he sees them. First he makes a list of complaints about Eclipse - constant refreshing, awkward UI building, hogging RAM, and so on - followed by a list of the improvements found in Android Studio, such as full-program themes, new UI tools, better stability and performance, and more. He does point to a couple of hiccups, such as the switch to a Gradle build, but it's more of a thing you're going to have to learn than an issue with Android Studio. Check out Donahue's full post for more details on the switch and the little things Android Studio does to make it more comfortable.
May 22, 2023
by Alec Noller
· 17,586 Views · 1 Like
article thumbnail
How to Get a Non-Programmer Started with R
This recent article from Alyssa Frazee's blog provides a tutorial on how to help non-programmers get started with R. Given that R is a programming language popular outside of the development world - its focus on statistics and data visualization makes it popular among data scientists and sociologists, for example - the tutorial is a useful starting point and provides an outline of the need-to-know aspects of R. A lot of ground is covered in a series of quick and concise steps. For instance: How to download R and RStudio Working with graphics Data types Exploratory data analysis And more. An important detail here, though, is the intended audience of the tutorial: It is not for non-programmers attempting to learn R, but really for programmers attempting to teach R to non-programmers, especially in a concise, crash-course fashion. I think there would definitely be some value here for non-programmers or even programmers who are new to R (though experienced programmers interested in R might be better served elsewhere), because it provides an outline of things you need to research and learn, but I believe the intention is to be more of an informal teaching aid. Check out the full tutorial for some insight on how to help a non-programmer get started with R.
May 22, 2023
by Alec Noller
· 9,401 Views · 1 Like
article thumbnail
How to Handle Secrets in Kubernetes
One crucial aspect of ensuring a secure Kubernetes infrastructure is the effective management of secrets, such as API keys, passwords, and tokens.
May 21, 2023
by Keshav Malik
· 2,653 Views · 2 Likes
article thumbnail
Using DuckDB With CockroachDB
Explore this fun experiment using DuckDB to parse CockroachDB Change Data Capture output and query CockroachDB with DuckDB.
May 21, 2023
by Artem Ervits DZone Core CORE
· 2,529 Views · 1 Like
article thumbnail
Build a Simple Chat Server With gRPC in .Net Core
Learn how to build a chat server using gRPC, a modern remote procedure call framework, and its support for streaming data.
May 19, 2023
by Okosodo Victor
· 11,314 Views · 4 Likes
article thumbnail
Building A Log Analytics Solution 10 Times More Cost-Effective Than Elasticsearch
Inverted indexing in Apache Doris 2.0.0 realizes two times faster log query performance than Elasticsearch with 1/5 of the storage space it uses.
May 19, 2023
by Shirley H.
· 5,392 Views · 4 Likes
article thumbnail
Which Is Better for IoT: Azure RTOS or FreeRTOS?
IoT needs speed, reliability, and energy efficiency that isn’t guaranteed in a desktop environment. Let's look at how to choose the right real-time operating system.
May 19, 2023
by Carsten Rhod Gregersen
· 9,703 Views · 1 Like
article thumbnail
Cypress Test Cases Execution With CI/CD GitHub Action
GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline.
May 19, 2023
by Kailash Pathak DZone Core CORE
· 3,552 Views · 2 Likes
article thumbnail
What Are the EKS Best Practices for Your SAAS Product?
In this blog, we will be looking at 10 AWS EKS best practices that will help you configure, deploy, use, and manage the Kubernetes Cluster on AWS.
May 19, 2023
by Rahul Shivalkar
· 4,648 Views · 2 Likes
article thumbnail
Clear Details on Java Collection ‘Clear()’ API
Several of us might be familiar with the clear () API in the Java collections framework. In this post, let’s discuss what is the purpose of this clear() API?
May 19, 2023
by Ram Lakshmanan DZone Core CORE
· 5,436 Views · 2 Likes
article thumbnail
PostgreSQL JSONB Cheatsheet: Complete and Fast Lookup Guide
This PostgreSQL JSONB Cheatsheet presents a complete guide to PostgreSQL JSONB functions with examples and some demo code.
May 19, 2023
by Francesco Tisiot
· 4,716 Views · 1 Like
article thumbnail
How to LINQ Between Java and SQL With JPAStreamer
Looking for a more streamlined and intuitive way of querying databases, similar to the elegant LINQ constructs in C#? Check out the open-source library JPAStreamer.
May 19, 2023
by Julia Gustafsson
· 10,201 Views · 12 Likes
article thumbnail
Playwright JavaScript Tutorial: A Complete Guide
The combination of Playwright and JavaScript allows you to create automated tests that can be run repeatedly and consistently, saving you time and effort.
May 19, 2023
by Kailash Pathak DZone Core CORE
· 9,114 Views · 2 Likes
article thumbnail
Apache Kafka vs. Message Queue: Trade-Offs, Integration, Migration
Message broker vs. data streaming — trade-offs, integration, and migration scenarios from JMS, IBM MQ, TIBCO, or ActiveMQ to Apache Kafka.
May 18, 2023
by Kai Wähner DZone Core CORE
· 5,266 Views · 2 Likes
article thumbnail
AWS Multi-Region Resiliency Aurora MySQL Global DB With Headless Clusters
In this article, the reader will learn how to perform a failover with a headless Aurora Global Database and its outcomes.
May 18, 2023
by Shiva Jayam
· 6,081 Views · 3 Likes
article thumbnail
Kubernetes Resource Limits: How To Make Limits and Requests Work for You
Learn to define resource limits and requests in Kubernetes, a key strategy to minimize cloud waste and effectively reduce your cloud expenditure.
May 18, 2023
by Darius Piekus
· 3,590 Views · 1 Like
article thumbnail
Why `fsync()`: Losing Unsynced Data on a Single Node Leads to Global Data Loss
Regardless of the replication mechanism, you must fsync() your data to prevent global data loss in non-Byzantine protocols.
May 18, 2023
by Denis Rystsov
· 2,839 Views · 1 Like
article thumbnail
The Native Way To Configure Path Aliases in Frontend Projects
We’ll take a look at the imports field in package.json and how it can be used to configure path aliases for various use cases.
May 18, 2023
by Maksim Zemskov
· 4,093 Views · 4 Likes
article thumbnail
Top 20 Git Commands With Examples
Learn and master these twenty essential Git commands for effective code management and collaboration with other developers.
May 18, 2023
by oliver liam
· 4,262 Views · 1 Like
  • Previous
  • ...
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • ...
  • Next
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook
×