From 2501731b527d4e168f295fcb0f7f00dbfdbc316b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=80=E5=90=89?= <84045975+thc1006@users.noreply.github.com> Date: Thu, 20 Aug 2026 21:23:24 +0800 Subject: [PATCH] Cancel previous request to avoid stale result (#7) Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com> --- src/script.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/script.js b/src/script.js index 808ed7c..282c84e 100644 --- a/src/script.js +++ b/src/script.js @@ -7,6 +7,9 @@ const status = document.querySelector("#status"); const rawResultsWrapper = document.querySelector("#rawResultsWrapper"); const rawResults = document.querySelector("#rawResults"); +// Keep the current request so a newer search can cancel the older one +let inflight = null; + // Parse URL query parameters const urlParams = new URLSearchParams(window.location.search); const user = urlParams.get('user'); @@ -20,6 +23,9 @@ if (user) { function find() { let user = gh.value.trim().toLowerCase(); if (user != "") { + // Cancel the previous request so an older slower response can't overwrite a newer one + if (inflight) inflight.abort(); + inflight = new AbortController(); statusUpdate(`Fetching devstats score for '${user}'`, "info"); rawResultsWrapper.classList.add('hidden'); fetch("https://devstats.cncf.io/api/v1", { @@ -30,6 +36,7 @@ function find() { "Content-Type": "application/json", }, redirect: "follow", + signal: inflight.signal, body: "{\"api\":\"GithubIDContributions\",\"payload\":{\"github_id\":\"" + user + "\"}}", }) .then(res => res.json()) @@ -45,6 +52,7 @@ function find() { rawResultsWrapper.classList.remove('hidden'); }) .catch(err => { + if (err.name === "AbortError") return; // request was replaced by a newer one statusUpdate(`Failed to get devstats score for '${user}'`, "error"); console.log(err); });