admin管理员组文章数量:1431460
I have a javascript that calls a function each 1 minute. That function sends a bot on the web looking for information to update on my database (quotes, to be exact).
So basically, sometime it's pretty fast and other times the servers dont answer fast... Let's say I have 100 seeks like this to make each 1 minute. They could take anywhere between 10 seconds to 1 minute+ to execute.
My question is : If the function is still fetching and/or waiting for data and I call it again (remember I call it each 1 minute), WILL IT CANCEL THE FIRST CALL to the function? If not, then will they simply stack and finish before starting the other one or will they run at the same time?
Thanks a bunch!
I have a javascript that calls a function each 1 minute. That function sends a bot on the web looking for information to update on my database (quotes, to be exact).
So basically, sometime it's pretty fast and other times the servers dont answer fast... Let's say I have 100 seeks like this to make each 1 minute. They could take anywhere between 10 seconds to 1 minute+ to execute.
My question is : If the function is still fetching and/or waiting for data and I call it again (remember I call it each 1 minute), WILL IT CANCEL THE FIRST CALL to the function? If not, then will they simply stack and finish before starting the other one or will they run at the same time?
Thanks a bunch!
Share Improve this question asked Jul 14, 2009 at 22:55 JoelJoel 8954 gold badges13 silver badges35 bronze badges 2- This doesn't strike me as something I would write as a broswer-based JS app. I'd be curious to hear more about your choice. – Brian Moeskau Commented Jul 15, 2009 at 0:56
- Thanks a lot for all your answers, you're great :) bmoeskau --> Well, since I never took programming courses, I use what I know :) So yes the page has to stay opened from 9h30 to 16h30 in order to work but I have an old low-power puter that does just that. The script has a bunch of triggers and warning that estimate when's the best time to buy and sell stocks and it works pretty fine! It uses PHP-MySQL-Jasvascript. I'd like to learn next server scripting so I don't need a browser to send my requests... – Joel Commented Jul 16, 2009 at 13:22
5 Answers
Reset to default 3Since you are on a single browser thread my guess is it will stack your calls
It sounds like the answer is "don't count on it." If you're using something like setTimeout to kick off your function, you're at the mercy of your javascript engine as to whether it will actually run your code concurrently or if it'll just block the second execution until the first one finishes up.
http://www.howtocreate.co.uk/tutorials/javascript/timers
Also, whatever happens, it CERTAINLY shouldn't cancel the execution of the running function.
Yes and no.
If your browser supports Web Workers then you can run multiple processes concurrently.
If it doesn't, then no you cannot run two processes at the same time. Even if you you setTimeout
and give it a delay of 0, it will not. Doing that is actually like a yield. Your function will execute as soon as the current stack is executed.
See How JavaScript Timers Work.
If you call your function using, setTimeout
, e.g.
setTimeout(fn, 60000);
then the calls to fn
will queue.
However, if your function makes an asynchronous HTTP request then potentially the responses will arrive out of order.
You may be interested in using Prototype's Function.PeriodicalExecuter to help you manage functions that take a long time to plete.
If including all of Prototype seems like overkill and you want to implement the functionality by hand, you could either have a boolean-flag that you set when an instance of the function is running; or a counter that is incremented at the head of the function, and decremented before it returns, giving you the total number of instances of that function.
If you are interested in using Prototype, another function that may help you out is Ajax.PeriodicalUpdater.
本文标签:
版权声明:本文标题:Javascript : can i automatically call a function many times even if it has not finished being executed? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745583268a2664745.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论