没隔一段时间轮询一次setInterval('play()',1000)

本文介绍了一个简单的 JavaScript 函数,该函数使用 console.log 打印数字 11,并通过 setInterval 实现每秒调用此函数。

function play(){
console.log(11)
}
setInterval(‘play()’,1000)

import { useEffect, useState } from "react"; interface State { is_listening: boolean; is_tts_playing: boolean; current_input_text: string; current_reply_text: string; last_command_result?: { response_to_user: string; tts_audio_url?: string; }; } function App() { const [state, setState] = useState<State>({ is_listening: false, is_tts_playing: false, current_input_text: "", current_reply_text: "", }); const API_BASE = "http://127.0.0.1:5000"; // 轮询获取当前状态 useEffect(() => { const interval = setInterval(async () => { try { const res = await fetch(`${API_BASE}/api/state`); const data = await res.json(); setState(data); } catch (err) { console.warn("无法连接到后端"); } }, 500); // 每 500ms 更新一次 return () => clearInterval(interval); }, []); // 切换录音状态 const toggleListening = async () => { try { await fetch(`${API_BASE}/api/state`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ is_listening: !state.is_listening }), }); } catch (err) { alert("无法连接到后端服务,请检查是否已启动!"); } }; return ( <div style={{ padding: "40px", fontFamily: "Arial" }}> <h1>🎙️ AI 语音助手</h1> <div> <button onClick={toggleListening} disabled={state.is_tts_playing} style={{ padding: "15px 30px", fontSize: "18px", backgroundColor: state.is_listening ? "#d32f2f" : "#4caf50", color: "white", border: "none", borderRadius: "8px", cursor: "pointer", }} > {state.is_listening ? "🛑 停止录音" : "🟢 开始录音"} </button> {state.is_tts_playing && <p>🔊 AI 正在说话…</p>} </div> <div style={{ marginTop: "30px" }}> <h3>你说:</h3> <p style={{ fontStyle: state.current_input_text ? "normal" : "italic", color: "#555" }}> {state.current_input_text || "等待语音输入…"} </p> <h3>AI 回答:</h3> <p style={{ fontStyle: state.current_reply_text ? "normal" : "italic", color: "#2196F3" }}> {state.current_reply_text || "等待 AI 回复…"} </p> </div> <footer style={{ marginTop: "50px", fontSize: "12px", color: "#aaa" }}> 后端状态轮询中 | API: {API_BASE} </footer> </div> ); } export default App; { "$schema": "../node_modules/@tauri-apps/cli/config.schema.json", "productName": "my-tauri-app", "version": "0.1.0", "identifier": "com.tauri.dev", "build": { "frontendDist": "../dist", "devUrl": "http://172.19.123.45:1573", "beforeDevCommand": "npm run dev", "beforeBuildCommand": "npm run build" }, "app": { "windows": [ { "title": "my-tauri-app", "width": 800, "height": 600, "resizable": true, "fullscreen": false, "x": 100, "y": 100 } ], "security": { "csp": null } }, "bundle": { "active": true, "targets": "all", "icon": [ "icons/icon-32.png", "icons/icon-128.png", "icons/icon-256x256.png", "icons/icon.icns", "icons/icon.ico" ] } }
最新发布
11-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值