← Torna al blog

Dev Tips

Un trucchetto al giorno leva le complicazioni di torno! Piccole "scorciatoie" che possono facilitare la vita degli sviluppatori e far risparmiare tempo...e righe di codice.

130

PerformanceObserver for monitoring web performance

2024-04-05

1```
2const observer = new PerformanceObserver((list) => {
3  const entries = list.getEntries();
4  entries.forEach(entry => {
5    console.log(`Resource timing for ${entry.name}: ${entry.duration}ms`);
6  });
7});
8
9observer.observe({ entryTypes: ['resource'] });
10```
129

navigator.sendBeacon for sending data in background

2024-02-15

1// Using navigator.sendBeacon for sending data in the background
2const data = "Analytics data";
3navigator.sendBeacon("/analytics", data);
4
5// Sending analytics at the end of a session
6document.addEventListener("visibilitychange", function logData() {
7  if (document.visibilityState === "hidden") {
8    navigator.sendBeacon("/analytics", data);
9  }
10});
128

Create a revocable Proxy

2024-01-16

1const target = { value: 42 };
2const { proxy, revoke } = Proxy.revocable(target, {});
3
4console.log(proxy.value); // Output: 42
5revoke();
6console.log(proxy.value); // Throws TypeError: Cannot perform 'get' on a proxy that has been revoked
127

Using Intl.RelativeTimeFormat for human-readable time

2023-12-14

1const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
2
3console.log(rtf.format(-1, "day")); // Output: yesterday
4console.log(rtf.format(2, "week")); // Output: in 2 weeks
5
6const rtf2 = new Intl.RelativeTimeFormat("it", { style: "short" });
7
8console.log(rtf2.format(3, "quarter")); // Output: tra 3 trim.
9console.log(rtf2.format(-1, "day")); // Output: 1 g fa
126

One-shot handling of an event

2023-10-29

1// <button id="one-shot-hero-button">Batman! Help me!</button>
2
3const fakeHeroEl = document.getElementById('one-shot-hero-button');
4fakeHeroEl.addEventListener('click', function(event) {
5  console.log('Call me one time and no more!');
6}, {once: true});
7
8fakeHeroEl.click() // Call me one time and no more!
9fakeHeroEl.click() // 

Contattaci.

Hai in mente un progetto e vorresti realizzarlo?
Sei interessato a migliorare le competenze del tuo team in ambito di programmazione e sviluppo?
Oppure vuoi semplicemente prendere prendere un caffè con noi e vedere la nostra collezione di Action Figure, allora scrivici tramite questo form.

Se, invece, vuoi far parte del team, guarda le nostre offerte di lavoro.

Questo sito è protetto da reCAPTCHA e si applicano le Norme sulla privacy e i Termini di servizio di Google.