Reputation: 1796
I have to call a function with the callback in set timeout for that I have written code like this
getData(a, b, function(err, rlt) {
if (err) {
console.log(err)
} else {
// call next function after 35 seconds
settimeout(getData(c, d, function(err, rlt) {
if (err) {
console.log(err)
} else {
// call next function after 10 seconds
settimeout(getData(x, y, function(err, rlt) {
if (err) {
console.log(err)
} else {
console.log(rlt);
}
}), 10000);
}
}), 35000)
}
});
function getData(parms1, parms2;, callback) {
return callback(null, parms1 + parms2);
}
I have written code similar to this but my problem is that set timeout not working its execute function immediately not wait for 35 seconds and 10 seconds.
I don't know what wrong I am doing and if you know any better way to do please help me.
Upvotes: 0
Views: 1963
Reputation: 355
I think you miss spell of settimeout(setTimeout) Try this code:
var callback = function (err, res) {
if(err){
console.log (err);
}else{
setTimeout(function() {
callback()
console.log ("start callback function");
}, 10000)
}
};
Upvotes: 0
Reputation: 7368
Below is the correct syntax for setTimeout
.Your method
should be always as a first argument of setTimeout
function
setTimeout(function(){myMethod(parameter);},3000);
Full Reference: http://www.java2s.com/Tutorials/Javascript/Node.js_Tutorial/0270__Node.js_setTimeout_setInterval.htm
Upvotes: 1
Reputation: 639
You must wrap your calls to getData in an anonymous function, e.g. setTimeout(function(){ getData(x, y, …) }, 1000)
.
Upvotes: 1