0

I have a cycle and I need this request to be sent every 3 seconds. How can do that?

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
})
user11220243
  • 55
  • 1
  • 7
  • Syntax error! You can use `setInterval` function. – hoangdv Mar 19 '19 at 04:09
  • I think the best thing is create a separate function for sending request. then put those things inside that function. then using `setTimeout(reqFun(), 3000);` method you can send a request – Darshana Pathum Mar 19 '19 at 04:10

2 Answers2

0
window.setInterval(function(){
  /// call your function here
}, 3000);

Please Try this JS code for send request in every three second

0

Put the code inside a function and call the function recursively with a timer

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
  })

}
Mohit Mutha
  • 2,921
  • 14
  • 25