我有一个周期,我需要每3秒发送一次此请求。该怎么办?
request({
url: `http://localhost:port/user/patch/${api.rows[j].id}`,
method: 'POST',
headers: {
'Cookie': cookies
}
},
function (error, response, body) {
console.log('patch:', body)
}).form({
Name: db.rows[i].NAME
})
答案 0 :(得分:0)
window.setInterval(function(){
/// call your function here
}, 3000);
请每三秒钟尝试发送此JS代码进行发送请求
答案 1 :(得分:0)
将代码放入函数中,并使用计时器递归调用该函数
function processData(){
let db = ///Get the data from somewhere
db.rows.forEach(row => {
setTimeout(() => myJob(row), 3000)
})
}
function myJob(row){
request({
url: `http://localhost:port/user/patch/${api.rows[j].id}`,
method: 'POST',
headers: {
'Cookie': cookies
}
},
function (error, response, body) {
console.log('patch:', body)
}).form({
Name: row.NAME
})
}