0

In my scenario, I have a mouse event. Where when I scroll the mouse everything it hits a function and respective function executes. I'm looking to check the execution time of the function and when If the execution time of the function exceeds a certain time, I want to stop the function. Is that possible without using setInterval/ setTimeOut!

Thanks in advance

  • 1
    Not possible. If a function is currently running, you cannot execute any other code. You could probably do something with Web Workers but it seems like a better idea to re-do the function so it doesn't execute for a very long time. – VLAZ Apr 15 '20 at 05:32
  • If the execution time of the function is too long! I want to cancel it sir @VLAZ –  Apr 15 '20 at 05:34
  • 1
    You can observe the execution time inside the function. – Teemu Apr 15 '20 at 05:35
  • 1
    @NoobDeveloper again, that's not possible to do from the outside. A function executing means *no other code can run*. – VLAZ Apr 15 '20 at 05:36
  • 1
    Post your code. There might be a solution. – Cully Apr 15 '20 at 05:37
  • https://jsfiddle.net/d67rz1j3/ –  Apr 15 '20 at 05:51
  • @Cully I have posted a sample –  Apr 15 '20 at 05:53
  • 1
    It's not clear what you actually need, maybe a [debouncer](https://stackoverflow.com/a/4298672/1169519)? – Teemu Apr 15 '20 at 05:55
  • @VLAZ Is that possible inside the function itself! I mean evaluating and to stop the function if the execution time is too long. But since! How can I interrupt inside a function when it's running. I can do it before the start of the function or end of the function right! –  Apr 15 '20 at 05:56
  • @Teemu Say when I scroll 4 times! and when I have a delay in it 2nd event, debounce jumps to the 4th event. But I guess the event only skip, but the functionalities of the 4 scroll events will occur in debounce –  Apr 15 '20 at 05:58
  • 1
    You can put a return everywhere in the function body, even multiple returns. But there's only a single line in your example, that function is executed within a nanosecond, that's probably not too long. – Teemu Apr 15 '20 at 05:58
  • @Teemu Yes it is a sample one sir! I agree –  Apr 15 '20 at 05:59
  • 2
    Could you please add a more realistic example to the post itself, make a stack snippet if possible. Currently it's really not clear what you need. – Teemu Apr 15 '20 at 06:01
  • 2
    @NoobDeveloper Could you update your question to include your code (and maybe make it more like what you're actually trying to do since it only has one line in the link you sent), and include some of the clarifications you've posted as comments? If you can give us a better idea of exactly what you want to do, it's likely there's a solution that works a bit differently from what you're asking for. Currently your question is too abstract to really help you find a concrete solution. – Cully Apr 15 '20 at 06:01
  • I have added my code @Teemu Here, Once the scroll event is over it goes through the function actionMouseWheel where the function takes more time to execute sometime! Where I'll be having other scrolls in queue too! So when the execution time is more, I want to stop the function! –  Apr 15 '20 at 06:13
  • @Cully I have posted my code –  Apr 15 '20 at 06:14
  • Any suggestions? –  Apr 15 '20 at 06:23
  • 1
    A debouncer would jump over the wheel ticks until the wheel stops for longer than the delay parameter says. If you need to count the ticks, you can add a counter to the debouncer code itself. That way the event handler would be executed only once per a series of the wheel ticks. If that's not what you want, you've to put a (hires) timestamp to the start of `actionMouseWheel`, and then check it against a new timestamp in suitable lines on `actionMouseWheel ` body code (not in the nested functions it contains). – Teemu Apr 15 '20 at 06:25
  • https://jsfiddle.net/h41wca0b/ @Teemu Updated code in the fiddle –  Apr 15 '20 at 06:30
  • @Teemu I have to evaluate the the timestamp in actionMouseWheel in the start of end or in the middle of the function –  Apr 15 '20 at 06:37
  • 1
    @NoobDeveloper What I mean is, edit your original question to include your code. Don't just post a link in the comments. Also, edit your code so that it's a better example of what you really want to do. I don't imagine you just want to change the font size to a constant on scroll. And if you could also edit your question to summarize the clarifications you posted in the comments. Basically, edit your question and improve it. Most people viewing this will not read all your comments. They'll just read your question. You're more likely to get an answer if you improve your question. – Cully Apr 15 '20 at 17:19

1 Answers1

0

Given that: we should better check if it actually removes the event (on jsfiddle I have doubts currently)

Could something like this fit?

html:

<div>
<p id="checks">
Waiting action
</p>
</div>

<div id="scroller">

In my scenario, I have a mouse event. Where when I scroll the mouse everything it hits a function and respective function executes. I'm looking to check the execution time of the function and when If the execution time of the function exceeds a certain time, I want to stop the function. Is that possible without using setInterval/ setTimeOut! 
In my scenario, I have a mouse event. Where when I scroll the mouse everything it hits a function and respective function executes. I'm looking to check the execution time of the function and when If the execution time of the function exceeds a certain time, I want to stop the function. Is that possible without using setInterval/ setTimeOut! 
In my scenario, I have a mouse event. Where when I scroll the mouse everything it hits a function and respective function executes. I'm looking to check the execution time of the function and when If the execution time of the function exceeds a certain time, I want to stop the function. Is that possible without using setInterval/ setTimeOut! 
In my scenario, I have a mouse event. Where when I scroll the mouse everything it hits a function and respective function executes. I'm looking to check the execution time of the function and when If the execution time of the function exceeds a certain time, I want to stop the function. Is that possible without using setInterval/ setTimeOut! 
In my scenario, I have a mouse event. Where when I scroll the mouse everything it hits a function and respective function executes. I'm looking to check the execution time of the function and when If the execution time of the function exceeds a certain time, I want to stop the function. Is that possible without using setInterval/ setTimeOut! 

</div>

Js:

var checks = document.getElementById('checks');
var scroller = document.getElementById('scroller');

scroller.addEventListener("scroll", () => {

   checks.innerHTML = "on scrolling";
   var scrolltimer = setInterval(myTimer, 1000);
   function myTimer()
   {
     checks.innerHTML = "stopping...";
     stopwork(scrolltimer,scroller);
   }

 },true);


function stopwork(scrolltimer,scroller)
{
  scroller.removeEventListener("scroll", null);
  clearTimeout(scrolltimer);
  checks.innerHTML = "stopped";
  return false;
}
Alberto
  • 274
  • 1
  • 5
  • 16