跳到正文
返回社区精选

文章

如何优雅的防止按钮重复点击

上帝之眼2025-06-30530 次浏览在社区查看
<div class="article-viewer markdown-body cache result"><h3 data-id="heading-0" style="box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, STHeiti, 微软雅黑, &quot;Microsoft YaHei&quot;, Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.5; color: rgb(51, 51, 51); margin: 10px 0px; font-size: 14px; padding: 0px;">1. 业务背景</h3><h3 data-id="heading-0"><div style="box-sizing: border-box; padding: 0px; margin: 0px; color: rgb(102, 102, 102); font-family: &quot;Helvetica Neue&quot;, STHeiti, 微软雅黑, &quot;Microsoft YaHei&quot;, Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><p style="box-sizing: border-box; margin: 10px 0px; padding: 0px; line-height: 1.5;">在前端的业务场景中:点击按钮,发起请求。在请求还未结束的时候,一个按钮可以重复点击,导致接口重新请求多次(如果后端不做限制)。轻则浪费服务器资源,重则业务逻辑错误,尤其是入库操作。</p><p style="box-sizing: border-box; margin: 10px 0px; padding: 0px; line-height: 1.5;">传统解决方案:使用防抖函数,但是无法解决接口响应时间过长的问题,当接口一旦响应时间超过防抖时间,测试单身20年的手速照样还是可以点击多次。</p><p style="box-sizing: border-box; margin: 10px 0px; padding: 0px; line-height: 1.5;">更稳妥的方式:给button添加loadng,只有接口响应结果后才能再次点击按钮。需要在每个使用按钮的页面逻辑中单独维护loading变量,代码变得臃肿。</p><p style="box-sizing: border-box; margin: 10px 0px; padding: 0px; line-height: 1.5;">那如果是在react项目中,这种问题有没有比较优雅的解决方式呢?</p><blockquote style="box-sizing: border-box; padding: 5px 10px; margin: 10px 0px; font-size: 14px; border-left: 8px solid rgb(208, 229, 242); color: rgb(119, 119, 119); display: block; line-height: 1.4; background-color: rgb(241, 241, 241);"><p style="box-sizing: border-box; margin: 10px 0px 0px; padding: 0px; line-height: 1.5;">vue项目解决方案参考:<a href="https://juejin.cn/post/7495414240360284197" target="_blank" title="https://juejin.cn/post/7495414240360284197" style="box-sizing: border-box; background: 0px 0px; color: rgb(21, 95, 170); text-decoration: none; cursor: pointer; padding: 0px; margin: 0px;">juejin.cn/post/749541…</a></p></blockquote></div></h3><h3 data-id="heading-1" style="box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, STHeiti, 微软雅黑, &quot;Microsoft YaHei&quot;, Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.5; color: rgb(51, 51, 51); margin: 10px 0px; font-size: 14px; padding: 0px;">2. useAsyncButton</h3><h3 data-id="heading-0"><div style="color: rgb(102, 102, 102);"><p>在 React 项目中,对于这种按钮重复点击的问题,可以使用自定义 Hook 来优雅地处理。以下是一个完整的解决方案:</p><div><ol><li>首先创建一个自定义 Hook&nbsp;<code style="font-size: 12.6px;">useAsyncButton</code>:</li></ol></div><pre style="color: rgb(77, 77, 76); background: white;"><code lang="typescript" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(137, 89, 168);">import</span> { useState, useCallback } <span style="color: rgb(137, 89, 168);">from</span> <span style="color: rgb(113, 140, 0);">'react'</span>; <span style="color: rgb(137, 89, 168);">interface</span> <span style="color: rgb(66, 113, 174);">RequestOptions</span> { onSuccess?: (<span style="color: rgb(245, 135, 31);">data: any</span>) =&gt; <span style="color: rgb(245, 135, 31);">void</span>; onError?: (<span style="color: rgb(245, 135, 31);">error: any</span>) =&gt; <span style="color: rgb(245, 135, 31);">void</span>; } <span style="color: rgb(137, 89, 168);">export</span> <span style="color: rgb(137, 89, 168);">function</span> <span style="color: rgb(66, 113, 174);">useAsyncButton</span><t>(<span style="color: rgb(245, 135, 31);"> </span><span style="color: rgb(245, 135, 31);">requestFn</span><span style="color: rgb(245, 135, 31);">: </span><span style="color: rgb(245, 135, 31);">(</span><span style="color: rgb(245, 135, 31);">...args: any[]</span><span style="color: rgb(245, 135, 31);">) =&gt;</span><span style="color: rgb(245, 135, 31);"> </span><span style="color: rgb(66, 113, 174);"><span style="color: rgb(245, 135, 31);">Promise</span></span><t><span style="color: rgb(245, 135, 31);">, </span><span style="color: rgb(245, 135, 31);">options</span><span style="color: rgb(245, 135, 31);">: </span><span style="color: rgb(66, 113, 174);"><span style="color: rgb(245, 135, 31);">RequestOptions</span></span><span style="color: rgb(245, 135, 31);"> = {} </span>) { <span style="color: rgb(137, 89, 168);">const</span> [loading, setLoading] = <span style="color: rgb(66, 113, 174);">useState</span>(<span style="color: rgb(245, 135, 31);">false</span>); <span style="color: rgb(137, 89, 168);">const</span> run = <span style="color: rgb(66, 113, 174);">useCallback</span>( <span style="color: rgb(137, 89, 168);">async</span> (...args: <span style="color: rgb(245, 135, 31);">any</span>[]) =&gt; { <span style="color: rgb(137, 89, 168);">if</span> (loading) <span style="color: rgb(137, 89, 168);">return</span>; <span style="color: rgb(142, 144, 140);">// 如果正在加载,直接返回</span> <span style="color: rgb(137, 89, 168);">try</span> { <span style="color: rgb(66, 113, 174);">setLoading</span>(<span style="color: rgb(245, 135, 31);">true</span>); <span style="color: rgb(137, 89, 168);">const</span> data = <span style="color: rgb(137, 89, 168);">await</span> <span style="color: rgb(66, 113, 174);">requestFn</span>(...args); options.onSuccess?.(data); <span style="color: rgb(137, 89, 168);">return</span> data; } <span style="color: rgb(137, 89, 168);">catch</span> (error) { options.onError?.(error); <span style="color: rgb(137, 89, 168);">throw</span> error; } <span style="color: rgb(137, 89, 168);">finally</span> { <span style="color: rgb(66, 113, 174);">setLoading</span>(<span style="color: rgb(245, 135, 31);">false</span>); } }, [loading, requestFn, options] ); <span style="color: rgb(137, 89, 168);">return</span> { loading, run }; } </t></t></code></pre><ol start="2"><li>在组件中使用这个 Hook:</li></ol></div><pre style="color: rgb(77, 77, 76); background: white;"><code lang="typescript" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(137, 89, 168);">import</span> { useAsyncButton } <span style="color: rgb(137, 89, 168);">from</span> <span style="color: rgb(113, 140, 0);">'../hooks/useAsyncButton'</span>; <span style="color: rgb(137, 89, 168);">const</span> <span style="color: rgb(66, 113, 174);">MyButton</span> = <span style="color: rgb(245, 135, 31);">(</span><span style="color: rgb(245, 135, 31);"></span><span style="color: rgb(245, 135, 31);">)</span> =&gt; { <span style="color: rgb(137, 89, 168);">const</span> { loading, run } = <span style="color: rgb(66, 113, 174);">useAsyncButton</span>(<span style="color: rgb(137, 89, 168);">async</span> () =&gt; { <span style="color: rgb(142, 144, 140);">// 这里是你的接口请求</span> <span style="color: rgb(137, 89, 168);">const</span> response = <span style="color: rgb(137, 89, 168);">await</span> <span style="color: rgb(66, 113, 174);">fetch</span>(<span style="color: rgb(113, 140, 0);">'your-api-endpoint'</span>); <span style="color: rgb(137, 89, 168);">const</span> data = <span style="color: rgb(137, 89, 168);">await</span> response.<span style="color: rgb(66, 113, 174);">json</span>(); <span style="color: rgb(137, 89, 168);">return</span> data; }, { onSuccess: (<span style="color: rgb(245, 135, 31);">data</span>) =&gt; { <span style="color: rgb(200, 40, 41);"><span style="color: rgb(245, 135, 31);">console</span></span>.<span style="color: rgb(66, 113, 174);">log</span>(<span style="color: rgb(113, 140, 0);">'请求成功:'</span>, data); }, onError: (<span style="color: rgb(245, 135, 31);">error</span>) =&gt; { <span style="color: rgb(200, 40, 41);"><span style="color: rgb(245, 135, 31);">console</span></span>.<span style="color: rgb(66, 113, 174);">error</span>(<span style="color: rgb(113, 140, 0);">'请求失败:'</span>, error); } }); <span style="color: rgb(137, 89, 168);">return</span> ( <span style="color: rgb(200, 40, 41);">&lt;button onClick=<span style="color: rgb(113, 140, 0);">{<span style="color: rgb(245, 135, 31);">()</span></span> =&gt;</span> run()} disabled={loading} &gt; {loading ? <span style="color: rgb(113, 140, 0);">'加载中...'</span> : <span style="color: rgb(113, 140, 0);">'点击请求'</span>} <span style="color: rgb(200, 40, 41);">button</span>&gt; ); }; <span style="color: rgb(137, 89, 168);">export</span> <span style="color: rgb(137, 89, 168);">default</span> <span style="color: rgb(66, 113, 174);">MyButton</span>; </code></pre><p style="color: rgb(102, 102, 102);">这个解决方案有以下优点:</p><div style="color: rgb(102, 102, 102);"><ol><li><span style="font-weight: 700;">统一管理</span>:将请求状态管理逻辑封装在一个 Hook 中,避免重复代码</li><li><span style="font-weight: 700;">自动处理 loading</span>:不需要手动管理 loading 状态</li><li><span style="font-weight: 700;">防重复点击</span>:在请求过程中自动禁用按钮或阻止重复请求</li><li><span style="font-weight: 700;">类型安全</span>:使用 TypeScript 提供类型检查</li><li><span style="font-weight: 700;">灵活性</span>:可以通过 options 配置成功/失败的回调函数</li><li><span style="font-weight: 700;">可复用性</span>:可以在任何组件中重用这个 Hook</li></ol></div><blockquote style="font-size: 14px;"><p><code style="font-size: 12.6px;">useAsyncButton</code>直接帮你进行了<code style="font-size: 12.6px;">try catch</code>,你不用再单独去做异常处理。</p></blockquote><p style="color: rgb(102, 102, 102);">是不是很简单?有的人可能有疑问了,为什么下方不就能拿到接口请求以后的数据吗?为什么还需要onSuccess呢?</p><pre style="color: rgb(77, 77, 76); background: white;"><code lang="typescript" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(137, 89, 168);">async</span> () =&gt; { <span style="color: rgb(142, 144, 140);">// 这里是你的接口请求</span> <span style="color: rgb(137, 89, 168);">const</span> response = <span style="color: rgb(137, 89, 168);">await</span> <span style="color: rgb(66, 113, 174);">fetch</span>(<span style="color: rgb(113, 140, 0);">'your-api-endpoint'</span>); <span style="color: rgb(137, 89, 168);">const</span> data = <span style="color: rgb(137, 89, 168);">await</span> response.<span style="color: rgb(66, 113, 174);">json</span>(); <span style="color: rgb(137, 89, 168);">return</span> data; } </code></pre></h3><h3 data-id="heading-2" style="font-size: 14px;">3. onSuccess</h3><h3 data-id="heading-0"><p style="color: rgb(102, 102, 102);">确实我们可以直接在调用&nbsp;<code style="font-size: 12.6px;">run()</code>&nbsp;后通过&nbsp;<code style="font-size: 12.6px;">.then()</code>&nbsp;或&nbsp;<code style="font-size: 12.6px;">await</code>&nbsp;来获取数据。提供&nbsp;<code style="font-size: 12.6px;">onSuccess</code>&nbsp;回调主要有以下几个原因:</p><div style="color: rgb(102, 102, 102);"><ol><li><span style="font-weight: 700;">关注点分离</span>:</li></ol></div><pre style="color: rgb(77, 77, 76); background: white;"><code lang="tsx" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(142, 144, 140);">// 不使用 onSuccess</span> <span style="color: rgb(137, 89, 168);">const</span> { run } = <span style="color: rgb(66, 113, 174);">useAsyncButton</span>(<span style="color: rgb(137, 89, 168);">async</span> () =&gt; { <span style="color: rgb(137, 89, 168);">const</span> response = <span style="color: rgb(137, 89, 168);">await</span> <span style="color: rgb(66, 113, 174);">fetch</span>(<span style="color: rgb(113, 140, 0);">'/api/data'</span>); <span style="color: rgb(137, 89, 168);">return</span> response.<span style="color: rgb(66, 113, 174);">json</span>(); }); <span style="color: rgb(137, 89, 168);">const</span> <span style="color: rgb(66, 113, 174);">handleClick</span> = <span style="color: rgb(137, 89, 168);">async</span> (<span style="color: rgb(245, 135, 31);"></span>) =&gt; { <span style="color: rgb(137, 89, 168);">const</span> data = <span style="color: rgb(137, 89, 168);">await</span> <span style="color: rgb(66, 113, 174);">run</span>(); <span style="color: rgb(142, 144, 140);">// 处理数据的逻辑和请求逻辑混在一起</span> <span style="color: rgb(66, 113, 174);">setData</span>(data); message.<span style="color: rgb(66, 113, 174);">success</span>(<span style="color: rgb(113, 140, 0);">'请求成功'</span>); <span style="color: rgb(66, 113, 174);">doSomethingElse</span>(data); }; <span style="color: rgb(142, 144, 140);">// 使用 onSuccess</span> <span style="color: rgb(137, 89, 168);">const</span> { run } = <span style="color: rgb(66, 113, 174);">useAsyncButton</span>(<span style="color: rgb(137, 89, 168);">async</span> () =&gt; { <span style="color: rgb(137, 89, 168);">const</span> response = <span style="color: rgb(137, 89, 168);">await</span> <span style="color: rgb(66, 113, 174);">fetch</span>(<span style="color: rgb(113, 140, 0);">'/api/data'</span>); <span style="color: rgb(137, 89, 168);">return</span> response.<span style="color: rgb(66, 113, 174);">json</span>(); }, { onSuccess: (<span style="color: rgb(245, 135, 31);">data</span>) =&gt; { <span style="color: rgb(142, 144, 140);">// 数据处理逻辑被清晰地分离出来</span> <span style="color: rgb(66, 113, 174);">setData</span>(data); message.<span style="color: rgb(66, 113, 174);">success</span>(<span style="color: rgb(113, 140, 0);">'请求成功'</span>); <span style="color: rgb(66, 113, 174);">doSomethingElse</span>(data); } }); <span style="color: rgb(137, 89, 168);">const</span> <span style="color: rgb(66, 113, 174);">handleClick</span> = <span style="color: rgb(245, 135, 31);">(</span><span style="color: rgb(245, 135, 31);"></span><span style="color: rgb(245, 135, 31);">)</span> =&gt; { <span style="color: rgb(66, 113, 174);">run</span>(); <span style="color: rgb(142, 144, 140);">// 更清晰的调用方式</span> }; </code></pre><ol start="2" style="color: rgb(102, 102, 102);"><li><span style="font-weight: 700;">统一错误处理</span>:</li></ol><pre style="color: rgb(77, 77, 76); background: white;"><code lang="tsx" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(142, 144, 140);">// 不使用 callbacks</span> <span style="color: rgb(137, 89, 168);">const</span> <span style="color: rgb(66, 113, 174);">handleClick</span> = <span style="color: rgb(137, 89, 168);">async</span> (<span style="color: rgb(245, 135, 31);"></span>) =&gt; { <span style="color: rgb(137, 89, 168);">try</span> { <span style="color: rgb(137, 89, 168);">const</span> data = <span style="color: rgb(137, 89, 168);">await</span> <span style="color: rgb(66, 113, 174);">run</span>(); <span style="color: rgb(66, 113, 174);">setData</span>(data); } <span style="color: rgb(137, 89, 168);">catch</span> (error) { <span style="color: rgb(142, 144, 140);">// 每个地方都需要写错误处理</span> message.<span style="color: rgb(66, 113, 174);">error</span>(<span style="color: rgb(113, 140, 0);">'请求失败'</span>); } }; <span style="color: rgb(142, 144, 140);">// 使用 callbacks</span> <span style="color: rgb(137, 89, 168);">const</span> { run } = <span style="color: rgb(66, 113, 174);">useAsyncButton</span>(fetchData, { onSuccess: (<span style="color: rgb(245, 135, 31);">data</span>) =&gt; <span style="color: rgb(66, 113, 174);">setData</span>(data), onError: (<span style="color: rgb(245, 135, 31);">error</span>) =&gt; message.<span style="color: rgb(66, 113, 174);">error</span>(<span style="color: rgb(113, 140, 0);">'请求失败'</span>) <span style="color: rgb(142, 144, 140);">// 错误处理被集中管理</span> }); </code></pre><ol start="3" style="color: rgb(102, 102, 102);"><li><span style="font-weight: 700;">自动重试场景</span>:</li></ol><pre style="color: rgb(77, 77, 76); background: white;"><code lang="tsx" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(137, 89, 168);">const</span> { run } = <span style="color: rgb(66, 113, 174);">useAsyncButton</span>(fetchData, { onSuccess: <span style="color: rgb(245, 135, 31);">(</span><span style="color: rgb(245, 135, 31);">data</span><span style="color: rgb(245, 135, 31);">)</span> =&gt; <span style="color: rgb(66, 113, 174);">setData</span>(data), onError: <span style="color: rgb(245, 135, 31);">(</span><span style="color: rgb(245, 135, 31);">error</span><span style="color: rgb(245, 135, 31);">)</span> =&gt; { <span style="color: rgb(137, 89, 168);">if</span> (retryCount &lt; <span style="color: rgb(245, 135, 31);">3</span>) { retryCount++; <span style="color: rgb(66, 113, 174);">run</span>(); <span style="color: rgb(142, 144, 140);"><span style="color: rgb(200, 40, 41);">//</span> 可以在失败时自动重试</span> } } }); </code></pre><ol start="4" style="color: rgb(102, 102, 102);"><li><span style="font-weight: 700;">状态联动</span>:</li></ol><pre style="color: rgb(77, 77, 76); background: white;"><code lang="tsx" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(137, 89, 168);">const</span> { run } = <span style="color: rgb(66, 113, 174);">useAsyncButton</span>(fetchData, { onSuccess: (<span style="color: rgb(245, 135, 31);">data</span>) =&gt; { <span style="color: rgb(66, 113, 174);">setData</span>(data); <span style="color: rgb(142, 144, 140);">// 可能需要触发其他请求</span> <span style="color: rgb(66, 113, 174);">refetchRelatedData</span>(); <span style="color: rgb(142, 144, 140);">// 或更新其他状态</span> <span style="color: rgb(66, 113, 174);">setOtherState</span>(<span style="color: rgb(245, 135, 31);">true</span>); } }); </code></pre><p style="color: rgb(102, 102, 102);">所以,虽然你完全可以不使用&nbsp;<code style="font-size: 12.6px;">onSuccess</code>&nbsp;回调,但它能帮助你:</p><ul style="color: rgb(102, 102, 102);"><li>更好地组织代码结构</li><li>统一管理成功/失败处理逻辑</li><li>方便进行状态联动</li><li>在需要扩展功能时更加灵活</li></ul><p style="color: rgb(102, 102, 102);">选择使用与否取决于你的具体需求,如果是简单的场景,直接使用&nbsp;<code style="font-size: 12.6px;">await run()</code>&nbsp;也完全可以。</p></h3><h3 data-id="heading-3" style="font-size: 14px;">4. 禁止一段时间内点击</h3><h3 data-id="heading-0"><p style="color: rgb(102, 102, 102);">评论区有人说了,我要是想在某一段时间内防止重复点击怎么整?</p><p style="color: rgb(102, 102, 102);">我们可以扩展&nbsp;<code style="font-size: 12.6px;">useAsyncButton</code>&nbsp;的功能,添加一个防冷却时间(cooldown)的特性。这在一些特定场景下很有用,比如发送验证码按钮需要等待 60 秒才能再次点击:</p><pre style="color: rgb(77, 77, 76); background: white;"><code lang="typescript" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(137, 89, 168);">import</span> { useState, useCallback, useRef } <span style="color: rgb(137, 89, 168);">from</span> <span style="color: rgb(113, 140, 0);">'react'</span>; <span style="color: rgb(137, 89, 168);">interface</span> <span style="color: rgb(66, 113, 174);">AsyncButtonOptions</span> { onSuccess?: (<span style="color: rgb(245, 135, 31);">data: any</span>) =&gt; <span style="color: rgb(245, 135, 31);">void</span>; onError?: (<span style="color: rgb(245, 135, 31);">error: any</span>) =&gt; <span style="color: rgb(245, 135, 31);">void</span>; cooldown?: <span style="color: rgb(245, 135, 31);">number</span>; <span style="color: rgb(142, 144, 140);">// 冷却时间(毫秒)</span> } <span style="color: rgb(137, 89, 168);">export</span> <span style="color: rgb(137, 89, 168);">function</span> <span style="color: rgb(66, 113, 174);">useAsyncButton</span><t>(<span style="color: rgb(245, 135, 31);"> </span><span style="color: rgb(245, 135, 31);">requestFn</span><span style="color: rgb(245, 135, 31);">: </span><span style="color: rgb(245, 135, 31);">(</span><span style="color: rgb(245, 135, 31);">...args: any[]</span><span style="color: rgb(245, 135, 31);">) =&gt;</span><span style="color: rgb(245, 135, 31);"> </span><span style="color: rgb(66, 113, 174);"><span style="color: rgb(245, 135, 31);">Promise</span></span><t><span style="color: rgb(245, 135, 31);">, </span><span style="color: rgb(245, 135, 31);">options</span><span style="color: rgb(245, 135, 31);">: </span><span style="color: rgb(66, 113, 174);"><span style="color: rgb(245, 135, 31);">AsyncButtonOptions</span></span><span style="color: rgb(245, 135, 31);"> = {} </span>) { <span style="color: rgb(137, 89, 168);">const</span> [loading, setLoading] = <span style="color: rgb(66, 113, 174);">useState</span>(<span style="color: rgb(245, 135, 31);">false</span>); <span style="color: rgb(137, 89, 168);">const</span> [cooldownRemaining, setCooldownRemaining] = <span style="color: rgb(66, 113, 174);">useState</span>(<span style="color: rgb(245, 135, 31);">0</span>); <span style="color: rgb(137, 89, 168);">const</span> timerRef = useRef&lt;<span style="color: rgb(66, 113, 174);">NodeJS</span>.Timeout&gt;(); <span style="color: rgb(137, 89, 168);">const</span> startCooldown = <span style="color: rgb(66, 113, 174);">useCallback</span>(<span style="color: rgb(245, 135, 31);">()</span> =&gt; { <span style="color: rgb(137, 89, 168);">if</span> (!options.cooldown) <span style="color: rgb(137, 89, 168);">return</span>; <span style="color: rgb(66, 113, 174);">setCooldownRemaining</span>(options.cooldown / <span style="color: rgb(245, 135, 31);">1000</span>); <span style="color: rgb(137, 89, 168);">const</span> startTime = <span style="color: rgb(66, 113, 174);"><span style="color: rgb(245, 135, 31);">Date</span></span>.<span style="color: rgb(66, 113, 174);">now</span>(); timerRef.current = <span style="color: rgb(245, 135, 31);">setInterval</span>(<span style="color: rgb(245, 135, 31);">()</span> =&gt; { <span style="color: rgb(137, 89, 168);">const</span> elapsed = <span style="color: rgb(66, 113, 174);"><span style="color: rgb(245, 135, 31);">Date</span></span>.<span style="color: rgb(66, 113, 174);">now</span>() - startTime; <span style="color: rgb(137, 89, 168);">const</span> remaining = <span style="color: rgb(66, 113, 174);"><span style="color: rgb(245, 135, 31);">Math</span></span>.<span style="color: rgb(66, 113, 174);">ceil</span>((options.cooldown! - elapsed) / <span style="color: rgb(245, 135, 31);">1000</span>); <span style="color: rgb(137, 89, 168);">if</span> (remaining &lt;= <span style="color: rgb(245, 135, 31);">0</span>) { <span style="color: rgb(245, 135, 31);">clearInterval</span>(timerRef.current); <span style="color: rgb(66, 113, 174);">setCooldownRemaining</span>(<span style="color: rgb(245, 135, 31);">0</span>); } <span style="color: rgb(137, 89, 168);">else</span> { <span style="color: rgb(66, 113, 174);">setCooldownRemaining</span>(remaining); } }, <span style="color: rgb(245, 135, 31);">1000</span>); }, [options.cooldown]); <span style="color: rgb(137, 89, 168);">const</span> run = <span style="color: rgb(66, 113, 174);">useCallback</span>( <span style="color: rgb(137, 89, 168);">async</span> (...args: <span style="color: rgb(245, 135, 31);">any</span>[]) =&gt; { <span style="color: rgb(137, 89, 168);">if</span> (loading || cooldownRemaining &gt; <span style="color: rgb(245, 135, 31);">0</span>) <span style="color: rgb(137, 89, 168);">return</span>; <span style="color: rgb(137, 89, 168);">try</span> { <span style="color: rgb(66, 113, 174);">setLoading</span>(<span style="color: rgb(245, 135, 31);">true</span>); <span style="color: rgb(137, 89, 168);">const</span> data = <span style="color: rgb(137, 89, 168);">await</span> <span style="color: rgb(66, 113, 174);">requestFn</span>(...args); options.onSuccess?.(data); <span style="color: rgb(66, 113, 174);">startCooldown</span>(); <span style="color: rgb(137, 89, 168);">return</span> data; } <span style="color: rgb(137, 89, 168);">catch</span> (error) { options.onError?.(error); <span style="color: rgb(137, 89, 168);">throw</span> error; } <span style="color: rgb(137, 89, 168);">finally</span> { <span style="color: rgb(66, 113, 174);">setLoading</span>(<span style="color: rgb(245, 135, 31);">false</span>); } }, [loading, cooldownRemaining, requestFn, options, startCooldown] ); <span style="color: rgb(137, 89, 168);">return</span> { loading, cooldownRemaining, run, disabled: loading || cooldownRemaining &gt; <span style="color: rgb(245, 135, 31);">0</span> }; } </t></t></code></pre><p style="color: rgb(102, 102, 102);">使用示例:</p><pre style="color: rgb(77, 77, 76); background: white;"><code lang="typescript" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(137, 89, 168);">import</span> { useAsyncButton } <span style="color: rgb(137, 89, 168);">from</span> <span style="color: rgb(113, 140, 0);">'../hooks/useAsyncButton'</span>; <span style="color: rgb(137, 89, 168);">const</span> <span style="color: rgb(66, 113, 174);">SendCodeButton</span> = <span style="color: rgb(245, 135, 31);">(</span><span style="color: rgb(245, 135, 31);"></span><span style="color: rgb(245, 135, 31);">)</span> =&gt; { <span style="color: rgb(137, 89, 168);">const</span> { loading, cooldownRemaining, disabled, run } = <span style="color: rgb(66, 113, 174);">useAsyncButton</span>( <span style="color: rgb(137, 89, 168);">async</span> () =&gt; { <span style="color: rgb(142, 144, 140);">// 发送验证码的接口请求</span> <span style="color: rgb(137, 89, 168);">const</span> response = <span style="color: rgb(137, 89, 168);">await</span> <span style="color: rgb(66, 113, 174);">fetch</span>(<span style="color: rgb(113, 140, 0);">'/api/send-code'</span>); <span style="color: rgb(137, 89, 168);">return</span> response.<span style="color: rgb(66, 113, 174);">json</span>(); }, { cooldown: <span style="color: rgb(245, 135, 31);">60000</span>, <span style="color: rgb(142, 144, 140);">// 60秒冷却时间</span> onSuccess: <span style="color: rgb(245, 135, 31);">()</span> =&gt; { <span style="color: rgb(200, 40, 41);"><span style="color: rgb(245, 135, 31);">console</span></span>.<span style="color: rgb(66, 113, 174);">log</span>(<span style="color: rgb(113, 140, 0);">'验证码发送成功'</span>); }, onError: (<span style="color: rgb(245, 135, 31);">error</span>) =&gt; { <span style="color: rgb(200, 40, 41);"><span style="color: rgb(245, 135, 31);">console</span></span>.<span style="color: rgb(66, 113, 174);">error</span>(<span style="color: rgb(113, 140, 0);">'验证码发送失败'</span>, error); } } ); <span style="color: rgb(137, 89, 168);">return</span> ( <span style="color: rgb(200, 40, 41);">&lt;button onClick=<span style="color: rgb(113, 140, 0);">{<span style="color: rgb(245, 135, 31);">()</span></span> =&gt;</span> run()} disabled={disabled} &gt; {loading ? <span style="color: rgb(113, 140, 0);">'发送中...'</span> : cooldownRemaining &gt; <span style="color: rgb(245, 135, 31);">0</span> ? <span style="color: rgb(113, 140, 0);">`${cooldownRemaining}秒后重试`</span> : <span style="color: rgb(113, 140, 0);">'发送验证码'</span>} <span style="color: rgb(200, 40, 41);">button</span>&gt; ); }; <span style="color: rgb(137, 89, 168);">export</span> <span style="color: rgb(137, 89, 168);">default</span> <span style="color: rgb(66, 113, 174);">SendCodeButton</span>; </code></pre><br style="color: rgb(102, 102, 102);"><div style="color: rgb(102, 102, 102);">作者:白哥学前端<br>来源:juejin.cn/post/7498646341460787211</div></h3></div>