1

I have a web site and i am using a javscript timer to swap images about.

I am using the timer like this:

var myTimer = window.setTimeout(MyFunction, MyInterval);

function MyFunction
{
//do something
//recalll timer
}

Now, the problem I have is not that the interval does not fire off at regular intervals as I can accept that in my application and I understand why it can vary.

The issue I have is that every now and then the timer stops for a few seconds and then resumes.

What I am trying ascertain is what is the main cause of this random suspension of the timer?

Is it due to the resources being diverted to another process on the hosting browser PC OR is it just the nature of using a JavaScript timer?

If the latter should I look to do an eternal loop? Everywhere I read and have practised elsewhere indicates that an infinite loop will grab all the resources and it would be a greater evil than the timer random suspension.

Are there any alternatives to using a javascript timer when a regular quick execution of code is paramount?

Thanks

6
  • 1
    Javascript runs on the client. Commented Dec 8, 2013 at 13:13
  • @SLaks Hi, thanks for pointing that out. I did know that I just did not put that in my question. I have corrected it. Thanks again Commented Dec 8, 2013 at 13:15
  • 1
    Hi, maybe setInterval() is what you need when you only need to change images every few seconds. Commented Dec 8, 2013 at 13:18
  • 1
    Since the same timer is called inside the callback, you can use setInterval instead setTimeout. A suggestion. :) Commented Dec 8, 2013 at 13:19
  • @FerdinandTorggler Hi, thanks for your interest in this question. I need to update images as quickly as possible and not every few seconds. What I meant about accepting the reliability of the interval is that there is nothing I can do about it. What I am trying to resolve are the random suspension of the timer. Commented Dec 8, 2013 at 13:20

2 Answers 2

2

The code you run inside MyFunction takes some time to execute (depending on what you are doing). When you recall the timer at the end of that function, the interval is not exactly MyInterval, because of the code execution time being added. If you use setInterval() instead of setTimeout(), the given function will be executed exactly every MyInterval milliseconds rather than (MyInterval + execution time) milliseconds.

To answer your question, the random suspension happens because of the execution time of your code.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi, thanks for reply. So, if the longish delay in seconds is down to the execution of my code and if i know the execution of my code is always doing the same thing would it then be down to the host of my server allocating resources to my domain space? Also if I use setInterval() will I not run the risk of displaying the images in a different order to when they were submitted?
The logic of fetching images will not change when you use setInterval over setTimeout. You might have to use a variable to keep track of the amout of images already fetched, or even an array of image-URLs every function called by setInterval has access to.
Hi again. If I call my image function and it is in that dreaded suspension cycle of a few seconds and If I use setInterval to call the image function again whilst still waiting for it to finish the 1st will I either experience the same delay as before whilst waiting for a return or will the 2nd call possibly over-seed the 1st call?
additional, they are not image-urls but byte array data from a call to an ashx page.
But the original question was what caused the delay? I am trying to determine whether a change of server would help or make no difference at all
|
1

I had a similar issue on a website I was working on and ultimately found the culprit in another timer-triggered job in a jQuery plugin that was occasionally delaying execution of my own function. If you're using external code in your site, you could do some debugging to see if this is your case too.

As a possible remedy, you could give a look at web workers. Since worker tasks are executed in a separated thread, they are not subject to delay when something in your main thread is taking too long to complete.

Your code would then look like this:

var worker = new Worker('worker.js');

And in another file called "worker.js" you would write:

var myTimer = setTimeout(MyFunction, MyInterval);

function MyFunction
{
//do something
//recalll timer
}

Just note that there is no window. anymore before setTimeout. This is because web workers don't have direct access to the DOM.

It's not guaranteed to solve your problem, but it's worth a test.

2 Comments

Hi, thank you for all that info. I am not alone. I shall give all that a go right now and see what happens, thanks!
Just to give some feedback on this. I had difficulyy returning 4 arguments to the Client. There are 4 image src to set you see and PostMessage gives me 1 option/argument. Anything else gave me an error though quite possible I was doing this wrong. What concerned me though was the speed tests reported elsewhere. Whilst I love the fact that data is extrapolated from the UI the process time actually increased. So not for me. But very interesting to look at.Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.