so I have the following code.
setInterval(function(){
steamOfferObj.getOffer({
"tradeOfferId": tradeOfferID["tradeofferid"] // The tradeoffer id
}, function(error, body) {
if (error == null) {
console.log(body);
if (body.response.offer.trade_offer_state == 3) {
return "Offer Accepted"
} else {
//on not accepted
}
}
});
}, 5000);
basically it poles a steam trade offer to see if it has completed or not. However, this actually runs indefinitely, checking every 5 seconds until the program is time. What I was is for it to check every 5 seconds, for 5 minutes, after which it times out.
Any way I could go about doing that?
You can use setTimeout
. For eg
var yourIntervalId = setInterval(function(){
steamOfferObj.getOffer({
"tradeOfferId": tradeOfferID["tradeofferid"] // The tradeoffer id
}, function(error, body) {
if (error == null) {
console.log(body);
if (body.response.offer.trade_offer_state == 3) {
return "Offer Accepted"
} else {
//on not accepted
}
}
});
}, 5000);
And here you clear the interval after 5 minutes (30000 ms)
setTimeout(function(){
clearInterval(yourIntervalId);
}, 30000);
with interval I would to use something like this:
var start = Date.now();
var theInterval = setInterval(function () {
if (Date.now() - start > 300000) {
clearInterval(theInterval);
return;
}
steamOfferObj.getOffer({
"tradeOfferId": tradeOfferID.tradeofferid // The tradeoffer id
}, function (error, body) {
if (error === null) {
console.log(body);
if (body.response.offer.trade_offer_state == 3) {
return "Offer Accepted";
} else {
//on not accepted
}
}
});
}, 5000);
but in this case I better to use setTimeout, because you using async request:
var start = Date.now();
function getData() {
if (Date.now() - start > 300000) {
return;
}
steamOfferObj.getOffer({
"tradeOfferId": tradeOfferID.tradeofferid // The tradeoffer id
}, function (error, body) {
setTimeout(getData, 5000);
if (error === null) {
console.log(body);
if (body.response.offer.trade_offer_state == 3) {
return "Offer Accepted";
} else {
//on not accepted
}
}
});
}
getData();
See 1-4
first, get start
time before setInterval called
second, save the timer_id
third, get current
time when the timer called
fourth, get the time difference, when it's exceed 5 minutes, stop the timer.
var start = (new Date()).getTime(); // 1
var timer_id = setInterval(function(){ // 2
var current = (new Date()).getTime(); // 3
if((current - start)/1000 > 5*60) clearInterval(timer_id); // 4
steamOfferObj.getOffer({
"tradeOfferId": tradeOfferID["tradeofferid"] // The tradeoffer id
}, function(error, body) {
if (error == null) {
console.log(body);
if (body.response.offer.trade_offer_state == 3) {
return "Offer Accepted"
} else {
//on not accepted
}
}
});
}, 5000);
Assign interval to a variable: var interval = setInterval; Before setInterval define current time: new Date(). Inside the interval check if 5 minutes passed. If it did, use: clearInterval(interval).
var now = new Date();
var intervalId = setInterval(function(){
var currNow = new Date();
if ((currNow-now) / 60000 > 5) {
clearInterval(intervalId );
return;
}
steamOfferObj.getOffer({
"tradeOfferId": tradeOfferID["tradeofferid"] // The tradeoffer id
}, function(error, body) {
if (error == null) {
console.log(body);
if (body.response.offer.trade_offer_state == 3) {
return "Offer Accepted"
} else {
//on not accepted
}
}
});
}, 5000);
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.