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);
}
}
javascript cocos2d-js
add a comment |
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);
}
}
javascript cocos2d-js
3
How often isupdate
being called currently? (Can you just dosetInterval(update, 60000)
?)
– CertainPerformance
Nov 20 at 5:57
3
look upsetInterval
and let us know if that solves your problem.
– Fallenreaper
Nov 20 at 5:57
1
@CertainPerformanceupdate
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
add a comment |
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);
}
}
javascript cocos2d-js
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
javascript cocos2d-js
edited Nov 20 at 5:59
Phil
95.1k11135154
95.1k11135154
asked Nov 20 at 5:56
Krissy
83
83
3
How often isupdate
being called currently? (Can you just dosetInterval(update, 60000)
?)
– CertainPerformance
Nov 20 at 5:57
3
look upsetInterval
and let us know if that solves your problem.
– Fallenreaper
Nov 20 at 5:57
1
@CertainPerformanceupdate
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
add a comment |
3
How often isupdate
being called currently? (Can you just dosetInterval(update, 60000)
?)
– CertainPerformance
Nov 20 at 5:57
3
look upsetInterval
and let us know if that solves your problem.
– Fallenreaper
Nov 20 at 5:57
1
@CertainPerformanceupdate
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
add a comment |
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 Date
s every frame would be to have a separate function that toggles the boolean, set with a setInterval
that runs every 60 seconds, no Date
s 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)
I imagineupdate
gets called viarequestAnimationFrame()
– Phil
Nov 20 at 6:16
Why not just a setInterval?
– Charlie Fish
Nov 20 at 6:17
add a comment |
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 Date
s every frame would be to have a separate function that toggles the boolean, set with a setInterval
that runs every 60 seconds, no Date
s 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)
I imagineupdate
gets called viarequestAnimationFrame()
– Phil
Nov 20 at 6:16
Why not just a setInterval?
– Charlie Fish
Nov 20 at 6:17
add a comment |
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 Date
s every frame would be to have a separate function that toggles the boolean, set with a setInterval
that runs every 60 seconds, no Date
s 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)
I imagineupdate
gets called viarequestAnimationFrame()
– Phil
Nov 20 at 6:16
Why not just a setInterval?
– Charlie Fish
Nov 20 at 6:17
add a comment |
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 Date
s every frame would be to have a separate function that toggles the boolean, set with a setInterval
that runs every 60 seconds, no Date
s 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)
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 Date
s every frame would be to have a separate function that toggles the boolean, set with a setInterval
that runs every 60 seconds, no Date
s 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)
edited Nov 20 at 6:21
answered Nov 20 at 6:15
CertainPerformance
70.2k143453
70.2k143453
I imagineupdate
gets called viarequestAnimationFrame()
– Phil
Nov 20 at 6:16
Why not just a setInterval?
– Charlie Fish
Nov 20 at 6:17
add a comment |
I imagineupdate
gets called viarequestAnimationFrame()
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
3
How often is
update
being called currently? (Can you just dosetInterval(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