I fetch data from Database in Kinvey using 2 javascript functions. I want the first function to be executed, and the next call to wait 2 seconds before executing. Everytime I use setTimeout I get: My error is :
TypeError: first._onTimeout is not a function
at Timer.listOnTimeout (timers.js:92:15)
My code is this:
router.post('/datacol/getuserdata', (req, res) => {
request(
{
url: kinveyurl + 'getPrivacyLevel',
method: 'POST',
json: {
"user_id" : req.body.user,
"sensors" : parseInt(req.body.sensor),
"contexts": parseInt(req.body.context),
"day_no" : parseInt(req.body.day),
"data_collectors": parseInt(req.user.type)
},
auth: {
user: config.kinvey.appKey,
pass: config.kinvey.masterSecret,
sendImmediately: true
}
}, ( err, resp, bod) => {
if(err) console.error(err)
console.log("User " + req.body.user + " Summarization Level = " + bod.summarization_level)
// This here should execute after 2 seconds!!!
// I try to set the timeout here!!
setTimeout(request(
{
url: kinveyurl + 'getAccelerometerData',
method: 'POST',
json: {
"user_id" : req.body.user,
"summarization_level": parseInt(bod.summarization_level),
"day_no" : parseInt(req.body.day)
},
auth: {
user: config.kinvey.appKey,
pass: config.kinvey.masterSecret,
sendImmediately: true
}
}, ( err, resp, sensorData) => {
if(err) console.error(err)
console.log(sensorData)
res.render('datacolaccelerometer', { user : req.user, sensordata: sensorData })
}),3000);
})
})
I am new to express and I do not know what to place where. In short, I would like to know where I should place the setTimeout so I can defer the execution of my second call to the javascript function by 2 seconds, thank you!
This is the correct way to use the setTimeout() as documented here
setTimeout(yourCallbackFunctionHere, 3000);
So, in your case use should use it like this:
setTimeout(function(){
//your express code here
}, 3000);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.