running async function once every minute











up vote
0
down vote

favorite












I was planning to get a data from the server every minute. However, if I run this code, this function is being called repeatedly. On the other hand, if I added date.getMilliseconds == 0 in the condition, it won't process any results.
Do you have any suggestions on how to run the function once every 1 minute?



async update() {
var date = new Date();
if (date.getSeconds() == 0) {
var newdata = await getData(1);
array.shift();
array.push(newdata);
}
}









share|improve this question




















  • 3




    How often is update being called currently? (Can you just do setInterval(update, 60000)?)
    – CertainPerformance
    Nov 20 at 5:57






  • 3




    look up setInterval and let us know if that solves your problem.
    – Fallenreaper
    Nov 20 at 5:57








  • 1




    @CertainPerformance update function is a lifecycle callback provided by cocos. it runs every frame (in milliseconds).
    – Krissy
    Nov 20 at 6:05










  • Lodash has some great methods for stuff like this.
    – Charlie Fish
    Nov 20 at 6:18















up vote
0
down vote

favorite












I was planning to get a data from the server every minute. However, if I run this code, this function is being called repeatedly. On the other hand, if I added date.getMilliseconds == 0 in the condition, it won't process any results.
Do you have any suggestions on how to run the function once every 1 minute?



async update() {
var date = new Date();
if (date.getSeconds() == 0) {
var newdata = await getData(1);
array.shift();
array.push(newdata);
}
}









share|improve this question




















  • 3




    How often is update being called currently? (Can you just do setInterval(update, 60000)?)
    – CertainPerformance
    Nov 20 at 5:57






  • 3




    look up setInterval and let us know if that solves your problem.
    – Fallenreaper
    Nov 20 at 5:57








  • 1




    @CertainPerformance update function is a lifecycle callback provided by cocos. it runs every frame (in milliseconds).
    – Krissy
    Nov 20 at 6:05










  • Lodash has some great methods for stuff like this.
    – Charlie Fish
    Nov 20 at 6:18













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I was planning to get a data from the server every minute. However, if I run this code, this function is being called repeatedly. On the other hand, if I added date.getMilliseconds == 0 in the condition, it won't process any results.
Do you have any suggestions on how to run the function once every 1 minute?



async update() {
var date = new Date();
if (date.getSeconds() == 0) {
var newdata = await getData(1);
array.shift();
array.push(newdata);
}
}









share|improve this question















I was planning to get a data from the server every minute. However, if I run this code, this function is being called repeatedly. On the other hand, if I added date.getMilliseconds == 0 in the condition, it won't process any results.
Do you have any suggestions on how to run the function once every 1 minute?



async update() {
var date = new Date();
if (date.getSeconds() == 0) {
var newdata = await getData(1);
array.shift();
array.push(newdata);
}
}






javascript cocos2d-js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 5:59









Phil

95.1k11135154




95.1k11135154










asked Nov 20 at 5:56









Krissy

83




83








  • 3




    How often is update being called currently? (Can you just do setInterval(update, 60000)?)
    – CertainPerformance
    Nov 20 at 5:57






  • 3




    look up setInterval and let us know if that solves your problem.
    – Fallenreaper
    Nov 20 at 5:57








  • 1




    @CertainPerformance update function is a lifecycle callback provided by cocos. it runs every frame (in milliseconds).
    – Krissy
    Nov 20 at 6:05










  • Lodash has some great methods for stuff like this.
    – Charlie Fish
    Nov 20 at 6:18














  • 3




    How often is update being called currently? (Can you just do setInterval(update, 60000)?)
    – CertainPerformance
    Nov 20 at 5:57






  • 3




    look up setInterval and let us know if that solves your problem.
    – Fallenreaper
    Nov 20 at 5:57








  • 1




    @CertainPerformance update function is a lifecycle callback provided by cocos. it runs every frame (in milliseconds).
    – Krissy
    Nov 20 at 6:05










  • Lodash has some great methods for stuff like this.
    – Charlie Fish
    Nov 20 at 6:18








3




3




How often is update being called currently? (Can you just do setInterval(update, 60000)?)
– CertainPerformance
Nov 20 at 5:57




How often is update being called currently? (Can you just do setInterval(update, 60000)?)
– CertainPerformance
Nov 20 at 5:57




3




3




look up setInterval and let us know if that solves your problem.
– Fallenreaper
Nov 20 at 5:57






look up setInterval and let us know if that solves your problem.
– Fallenreaper
Nov 20 at 5:57






1




1




@CertainPerformance update function is a lifecycle callback provided by cocos. it runs every frame (in milliseconds).
– Krissy
Nov 20 at 6:05




@CertainPerformance update function is a lifecycle callback provided by cocos. it runs every frame (in milliseconds).
– Krissy
Nov 20 at 6:05












Lodash has some great methods for stuff like this.
– Charlie Fish
Nov 20 at 6:18




Lodash has some great methods for stuff like this.
– Charlie Fish
Nov 20 at 6:18












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Since it looks like you don't have fine control over when update is called, one option would be to set a boolean to true every time getSeconds() === 0 (set it to false otherwise), and then only run the real code when the flag is false and getSeconds() === 0:



let hasRun = false;
// ...
async update() {
var date = new Date();
const secs = date.getSeconds();
if (secs !== 0) {
hasRun = false;
} else if (secs === 0 && hasRun === false) {
hasRun = true;
// your code
var newdata = await getData(1);
array.shift();
array.push(newdata);
}
}


An alternative that might require less resources due to not creating Dates every frame would be to have a separate function that toggles the boolean, set with a setInterval that runs every 60 seconds, no Dates involved:



let hasRun = false;
setInterval(() => hasRun = false, 60000);
async update() {
if (hasRun) return;
hasRun = true;
// your code
}


(of course, you could also try setInterval(update, 60000) if the framework allows it)






share|improve this answer























  • I imagine update gets called via requestAnimationFrame()
    – Phil
    Nov 20 at 6:16












  • Why not just a setInterval?
    – Charlie Fish
    Nov 20 at 6:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387046%2frunning-async-function-once-every-minute%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Since it looks like you don't have fine control over when update is called, one option would be to set a boolean to true every time getSeconds() === 0 (set it to false otherwise), and then only run the real code when the flag is false and getSeconds() === 0:



let hasRun = false;
// ...
async update() {
var date = new Date();
const secs = date.getSeconds();
if (secs !== 0) {
hasRun = false;
} else if (secs === 0 && hasRun === false) {
hasRun = true;
// your code
var newdata = await getData(1);
array.shift();
array.push(newdata);
}
}


An alternative that might require less resources due to not creating Dates every frame would be to have a separate function that toggles the boolean, set with a setInterval that runs every 60 seconds, no Dates involved:



let hasRun = false;
setInterval(() => hasRun = false, 60000);
async update() {
if (hasRun) return;
hasRun = true;
// your code
}


(of course, you could also try setInterval(update, 60000) if the framework allows it)






share|improve this answer























  • I imagine update gets called via requestAnimationFrame()
    – Phil
    Nov 20 at 6:16












  • Why not just a setInterval?
    – Charlie Fish
    Nov 20 at 6:17















up vote
1
down vote



accepted










Since it looks like you don't have fine control over when update is called, one option would be to set a boolean to true every time getSeconds() === 0 (set it to false otherwise), and then only run the real code when the flag is false and getSeconds() === 0:



let hasRun = false;
// ...
async update() {
var date = new Date();
const secs = date.getSeconds();
if (secs !== 0) {
hasRun = false;
} else if (secs === 0 && hasRun === false) {
hasRun = true;
// your code
var newdata = await getData(1);
array.shift();
array.push(newdata);
}
}


An alternative that might require less resources due to not creating Dates every frame would be to have a separate function that toggles the boolean, set with a setInterval that runs every 60 seconds, no Dates involved:



let hasRun = false;
setInterval(() => hasRun = false, 60000);
async update() {
if (hasRun) return;
hasRun = true;
// your code
}


(of course, you could also try setInterval(update, 60000) if the framework allows it)






share|improve this answer























  • I imagine update gets called via requestAnimationFrame()
    – Phil
    Nov 20 at 6:16












  • Why not just a setInterval?
    – Charlie Fish
    Nov 20 at 6:17













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Since it looks like you don't have fine control over when update is called, one option would be to set a boolean to true every time getSeconds() === 0 (set it to false otherwise), and then only run the real code when the flag is false and getSeconds() === 0:



let hasRun = false;
// ...
async update() {
var date = new Date();
const secs = date.getSeconds();
if (secs !== 0) {
hasRun = false;
} else if (secs === 0 && hasRun === false) {
hasRun = true;
// your code
var newdata = await getData(1);
array.shift();
array.push(newdata);
}
}


An alternative that might require less resources due to not creating Dates every frame would be to have a separate function that toggles the boolean, set with a setInterval that runs every 60 seconds, no Dates involved:



let hasRun = false;
setInterval(() => hasRun = false, 60000);
async update() {
if (hasRun) return;
hasRun = true;
// your code
}


(of course, you could also try setInterval(update, 60000) if the framework allows it)






share|improve this answer














Since it looks like you don't have fine control over when update is called, one option would be to set a boolean to true every time getSeconds() === 0 (set it to false otherwise), and then only run the real code when the flag is false and getSeconds() === 0:



let hasRun = false;
// ...
async update() {
var date = new Date();
const secs = date.getSeconds();
if (secs !== 0) {
hasRun = false;
} else if (secs === 0 && hasRun === false) {
hasRun = true;
// your code
var newdata = await getData(1);
array.shift();
array.push(newdata);
}
}


An alternative that might require less resources due to not creating Dates every frame would be to have a separate function that toggles the boolean, set with a setInterval that runs every 60 seconds, no Dates involved:



let hasRun = false;
setInterval(() => hasRun = false, 60000);
async update() {
if (hasRun) return;
hasRun = true;
// your code
}


(of course, you could also try setInterval(update, 60000) if the framework allows it)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 6:21

























answered Nov 20 at 6:15









CertainPerformance

70.2k143453




70.2k143453












  • I imagine update gets called via requestAnimationFrame()
    – Phil
    Nov 20 at 6:16












  • Why not just a setInterval?
    – Charlie Fish
    Nov 20 at 6:17


















  • I imagine update gets called via requestAnimationFrame()
    – Phil
    Nov 20 at 6:16












  • Why not just a setInterval?
    – Charlie Fish
    Nov 20 at 6:17
















I imagine update gets called via requestAnimationFrame()
– Phil
Nov 20 at 6:16






I imagine update gets called via requestAnimationFrame()
– Phil
Nov 20 at 6:16














Why not just a setInterval?
– Charlie Fish
Nov 20 at 6:17




Why not just a setInterval?
– Charlie Fish
Nov 20 at 6:17


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387046%2frunning-async-function-once-every-minute%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'