TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Debounce in Bash

20 pointsby vackosarover 6 years ago

5 comments

cryptonectorover 6 years ago
When measuring elapsed times, instead of executing date(1) to get the current time, use $SECONDS:<p><pre><code> SECONDS Each time this parameter is referenced, the number of seconds since shell invocation is returned. If a value is assigned to SECONDS, the value returned upon subsequent references is the number of seconds since the assignment plus the value assigned. If SECONDS is unset, it loses its special properties, even if it is subsequently reset. </code></pre> Like so:<p><pre><code> throttle() { local -i limit ((limit = SECONDS + $1)) while read line; do if ((limit &lt; SECONDS)); then ((limit = SECONDS + $1)) echo &quot;$line&quot; fi done } </code></pre> Try it like so:<p><pre><code> $ yes | throttle 2 </code></pre> Of course, that&#x27;s a bad example in that yes(1) will go as fast as you let it and it always outputs the same thing. In the case of yes(1) the better thing to do would be to sleep between reads, but yes(1) is a special case.
评论 #18062479 未加载
ganonmover 6 years ago
The term debouncing is also used in an analogous manner for electromechanical switches and is probably the origin of the term. These switches literally &#x27;bounce&#x27; off the contact during the first few milliseconds after the circuit is closed, rapidly opening and closing the circuit. Debouncing attempts to filter out these rapid, unwanted changes by e.g. adding a capacitor in parallel.
评论 #18065401 未加载
wesleytoddover 6 years ago
&gt;For example it exists both in React and JavaScript<p>There is a funny hot take in there somewhere...
评论 #18064014 未加载
评论 #18082243 未加载
aenigmaclamoover 6 years ago
This seems more like a throttle rather than a debounce:<p><a href="https:&#x2F;&#x2F;css-tricks.com&#x2F;debouncing-throttling-explained-examples&#x2F;" rel="nofollow">https:&#x2F;&#x2F;css-tricks.com&#x2F;debouncing-throttling-explained-examp...</a>
KirinDaveover 6 years ago
Good script.<p>I have this touchpoint problem too. Since I hate those red eraser nubs anyways, I actually disable them entirely at the xinput level. This breaks the physical right mouse button. (because Linux, ofc) but fortunately the touchpad driver replaces it.<p>Better overall experience without any chance of pausing as the driver bounces back.