3 回答

TA贡献1836条经验 获得超4个赞
这是管理 ping 从 3 秒到 5 秒的转换的东西。
const firstPing = 3000,
pingStop = 6000,
pingForever = 5000;
let currentPing = 0;
let ping = setInterval(function() { execute() }, firstPing);
function execute() {
console.log('hello: ' + new Date().getSeconds());
// After successful response, clearInterval();
if(++currentPing >= 2 ) {
clearInterval(ping);
ping = setInterval(() => execute(), pingForever);
}
}

TA贡献1788条经验 获得超4个赞
每 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();

TA贡献1773条经验 获得超3个赞
我只是setTimeout为了简单而使用。
var found = null;
function tryRequest() {
if (found) return;
// send request
// in successful response, set 'found' to 'true'
setTimeout(tryRequest, found == null ? 3000 : 5000);
found = false;
}
setTimeout(tryRequest, 3000);
添加回答
举报