gpt4 book ai didi

javascript每3秒设置一次间隔时间,前6秒,然后连续5秒,直到我收到答案

转载 作者:行者123 更新时间:2023-12-01 00:55:48 25 4
gpt4 key购买 nike

我想在前 6 秒内每 3 秒 ping 一次服务器以获取响应,之后我想将间隔时间增加到 5 秒,直到获得响应。我做了第一部分,我正在尝试解决接下来的 5 秒 ping

var firstPing = 3000,
pingStop = 6000,
pingForever = 5000;
var ping = setInterval(function() { execute() }, firstPing);

setTimeout(function() {clearInterval(ping)}, pingStop);

setInterval(function() {execute()}, pingForever);

function execute() {
console.log('hello: ' + new Date().getSeconds());
// After successful response, clearInterval();

}

最佳答案

每 1 秒调用一次execute(),并且仅当递增计数器变量达到某个值时才让execute 执行某些操作,这样可能会更简单吗?

var ping = setInterval(function() { execute() }, 1000);

let v = 0;
function execute() {
v++;
if(v==3 || v==6 || (v>6 && v%5 == 1))
console.log('hello: ' + new Date().getSeconds());
// After successful response, clearInterval();

`

关于javascript每3秒设置一次间隔时间,前6秒,然后连续5秒,直到我收到答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56591179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com