Every keystroke in the link box on ramrom.io used to cost me money. Not much money — a fraction of a cent per call — but the logic was wrong, and wrong logic on a free tool adds up fast.
The problem was simple. A visitor pastes a TikTok or Instagram link, and my JavaScript fires a resolve call to the server, which then spends one of my paid API calls to fetch the video details. If the visitor types the link by hand, or pastes it and then edits it — fixing a typo, deleting a stray space, trimming a tracking parameter — every change fired another resolve. Ten keystrokes, ten API calls, nine of them for links nobody ever wanted.
Worse, the calls raced each other. A slow response for an old, half-typed link could arrive after the response for the finished link, and the page would show the wrong result. The visitor would see download options for a link they had already corrected. That is the kind of bug that looks like the tool is broken when the tool is actually working.
The fix is debouncing, and it is one of the smallest changes I have made to the site. Instead of calling resolve on every input event, I wait. When the visitor types, I start a timer — 400 milliseconds. If they type again before the timer finishes, I cancel it and start a new one. The resolve call only fires when they have stopped typing for 400 milliseconds. That is it. No library, no framework, just setTimeout and clearTimeout.
Here is the shape of it:
let resolveTimer = null;
linkInput.addEventListener('input', () => {
clearTimeout(resolveTimer);
resolveTimer = setTimeout(() => {
resolveLink(linkInput.value.trim());
}, 400);
});
The paste event gets the same treatment, because pasting is where the worst waste happened. A paste fires an input event too, so the same timer covers it — but I also trim the value before resolving, so a pasted link with a trailing newline or a space does not fire a call for a broken URL and then another for the fixed one. Cleaning the input first, then debouncing, cut the wasted calls to almost nothing.
There is one more piece that matters. Even with debouncing, an old request can still be in flight when a newer one finishes. I keep a counter that goes up with every resolve call, and each response carries the counter value it was sent with. When a response arrives, I check whether its number is still the latest. If it is not, I throw it away. That is five lines of code, and it ends the race condition completely.
let latestRequest = 0;
async function resolveLink(url) {
const myRequest = ++latestRequest;
const result = await fetch('/api/resolve.php', {
method: 'POST',
body: JSON.stringify({ url })
}).then(r => r.json());
if (myRequest !== latestRequest) return; // a newer request exists
showResults(result);
}
Why 400 milliseconds? It is a judgement call, not a magic number. Shorter, and a slow typist still fires several calls. Longer, and the tool feels lazy — the visitor pastes a link and waits, wondering if anything is happening. I tested 300, 400 and 600 on my own phone, and 400 felt right: fast enough that a paste resolves almost at once, slow enough that typing never fires a call mid-word.
I also show a small loading state the moment the timer starts, not when the request fires, so the visitor knows something is coming. The difference between a tool that feels broken and a tool that feels fast is often just that one signal.
The result is boring in the best way. One paste, one API call. One typed link, one API call. The quota I used to burn on half-typed URLs now goes to real downloads, and the page never shows results for a link the visitor has already fixed. If you run any tool that calls a paid API from a text input, add the timer before you add anything else — it is the cheapest saving on the whole site.
Top comments (0)