A Simple Slideshow Web Component

As I continue to play around with and learn more about web components, I thought I’d build a simple component to make it easier to add a slideshow. By that, I mean something that renders one picture but provides controls to go to more images. I’ve probably built this many times in the past, both in JavaScript and server-side code, and I thought it would be a nice candidate for a component. As with most of my demos so far, there’s a lot more that could be done with it, but I thought I’d share what I have so far. Once again I want to give a shout-out to Simon MacDonald for helping me get this code working. (At the end of the post, I’ll share the mistake I made, as... more →
Posted in: JavaScript

fetch with Timeout

A few years back I wrote a blog post about how write a fetch Promise that times out. The function was effective but the code wasn’t great, mostly because AbortController , which allows you to cancel a fetch Promise, did not yet exist. With AbortController and AbortSignal available, let’s create a better JavaScript function for fetching with a timeout: Much like the original function, we’ll use setTimeout to time to the cancellation but we’ll use the signal with the fetch request: async function fetchWithTimeout(url, opts = {}, timeout = 5000) { // Create the AbortController instance, get AbortSignal const abortController = new AbortController(); const { signal }... more →
Posted in: JavaScript

A Dialect Map of England

The University of Leeds’ Dialect and Heritage Project has released a Sound Map of English dialects. The map features archived audio recordings of native English speakers from the different regions of England. In the 1950s and 1960s the Survey of English Dialects undertook to complete a survey of the regional dialects of England. The survey was conducted in over 300 different towns and villages. Maps Mania… more →
Posted in: Interactive Maps

Followup to My Intl Short Number Post

A few days ago I shared a blog post about using the Intl object in JavaScript to create short, more readable numbers. So for example, instead of 9123456, it would display 9.1M. This was done using the notation option in Intl.NumberFormat. Yesterday I randomly ran into an interesting modification on this using yet another option, compactDisplay. The compactDisplay option is only used when notation is set to compact. It supports two options, short which is default and what I demonstrated in the previous post, and long. So given a number, i, you would use it like so: new Intl.NumberFormat('en-US', { notation:'compact', compactDisplay:'long'}).format(i); And the result is, well, longer. 😉 What’s... more →
Posted in: JavaScript

The Campaign for More Winter Sun

Today the residents of New York will see less than nine and a half hours of daylight. This lack of sunlight can lead to many people feeling SAD. Which is partly why every year there are calls to end the annual switch to standard time from daylight saving time, the result of which is that the sun sets even earlier every cold winter day. It is also why FiveThirtyEight  has asked the question Can Maps Mania… more →
Posted in: Interactive Maps

Short Number Formatting in Python

Yesterday I wrote a blog post about creating short number formats in JavaScript. Definitely check out that post first, but the idea was to take something like 9496301 and display it as 9.5M. In that post, I used the built-in Intl object and it worked really well. It got me thinking, could you do the same in Python? First off, I checked and was happy to see that like JavaScript, Python supports numeric separators. This makes it much easier to read large numbers in code. It also meant I could take my test array and copy and paste it into a Python program: inputs = [ 999, 1000, 2999, 12_499, 12_500, 430912, 9_123_456, 1_111_111_111, 81_343_902_530, 1_111_111_111_111, 62_123_456_789_011,... more →
Posted in: JavaScript

The Land of Generation X

Modal Age by Neighbourhood is an interactive map which visualizes the median age of the population in English and Welsh neghborhoods (middle-layer super output areas), using data from the 2021 census. The map provides a fascinating insight into the average ages of the countries’ towns and cities (a Median Age map is also available).By area of land (but not necessarily by population) England and Maps Mania… more →
Posted in: Interactive Maps

How to Determine a JavaScript Promise’s Status

Promises have changed the landscape of JavaScript. Many old APIs have been reincarnated to use Promises (XHR to fetch, Battery API), while new APIs trend toward Promises. Developers can use async/await to handle promises, or then/catch/finally with callbacks, but what Promises don’t tell you is their status. Wouldn’t it be great if the Promise.prototype provided developers a status property to know whether a promise is rejected, resolved, or just done? My research led me to this gist which I found quite clever. I took some time to modify a bit of code and add comments. The following solution provides helper methods for determining a Promise’s status: // Uses setTimeout with... more →
Posted in: JavaScript
1 48 49 50 51 52 190