How to use unit of time less than milliseconds in a setInterval or setTimeout

Asked

Viewed 521 times

-2

I would like to know (if possible) how to use a unit of time less than milliseconds in the functions setInterval or setTimeout.

I made a progressive stopwatch using the following code

function timer(){
    if(counter < qntLinhas){
        counter++;
        $("#quantidade").text(counter);
    }
    if(counter > qntLinhas){
        counter -= 1;
        $("#quantidade").text(counter);
    }
}

setInterval("timer()", 0);

The variable qntLinhas is referring to the result of an AJAX query that returns me an X value.

It works very well, However, when the value of qntLines is very high, it takes a little to get the final result and, even be boring to wait.

To circumvent this, I made two increments in a row in the counter variable, but in addition to being a gambiarra, the final result is not necessary.

I tried using the plugin countTo and the question of time was resolved, but another problem arose. It does not allow me to update the value, which I want to achieve in the count, at runtime (via AJAX).

  • 2

    Can you explain further the idea of progressive chronometer and how it should interact with ajax? to be honest I don’t understand what problem you’re trying to solve

  • Why are you using 0 in the timer? It’s only by the desired value there. For example 60000 for a minute.

  • http://clayderson.com.br/ take a look at this page, you’ll understand. the end value of the counter is set at run time with ajax. That chronometer is updated every 30 seconds with a new value (if applicable).

  • @Bacco, I want a value less than 0 milliseconds, not higher.

  • 4

    If you just want the answer to what you asked, it’s simple: "there’s no way". But I’m pretty sure the problem isn’t in the timer, only it’s hard to help without you explaining the problem that has to be solved, rather than the way you think it’s going to solve. Reading suggestion: What is the XY problem. Once we know the problem, we can propose solutions without needing the timer less than milliseconds.

  • 2

    PS: If it’s the comment counter, it’s even simpler, just don’t count from 1 to 1, and make an exponential curve to be cool (starting to skip a larger amount, and decreasing the interval when getting close to the value). And in this case, edit the question and add this detail, because in the comment normally visitors will not be looking for details.

Show 1 more comment

2 answers

7


The question has been answered here, and already received my +1. I will complement with this answer, which refers to the real problem, which was not initially posted in the body of the question.

For the counter not to take too long, I made a formula that calculates the increment step according to the distance remaining to reach the total value:

var qntLinhas = 938203;
var counter = 0;

function timer(){
    if(counter < qntLinhas){
        counter += Math.min( Math.ceil( ( qntLinhas - counter ) / 10 ), counter + 1);
        $("#quantidade").text(counter);
        setTimeout("timer()", 50);
    }
}
timer();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div id="quantidade"></div>

In order for the processing not to happen randomly, we changed the setInterval for setTimeout, which will only be called as long as the counter needs to increment.

When completing Ajax, simply call timer() to update the counter.

To adjust the time, just touch the / 10 of the formula, and not setTimeOut. Formula splitting adjusts counter speed without having to overload page processing with lower timeout.


Test with very different values

This one I posted only as a test, to show how the formula adapts to very different values, with little difference between the finishes:

var qntLinhas1 = 1003;
var qntLinhas2 = 389041;
var qntLinhas3 = 93820317;
var counter1 = 0;
var counter2 = 0;
var counter3 = 0;

function timer(){
   if(counter1 < qntLinhas1) counter1 += Math.min( Math.ceil( ( qntLinhas1 - counter1 ) / 10 ), counter1 + 1);
   if(counter2 < qntLinhas2) counter2 += Math.min( Math.ceil( ( qntLinhas2 - counter2 ) / 10 ), counter2 + 1);
   if(counter3 < qntLinhas3) counter3 += Math.min( Math.ceil( ( qntLinhas3 - counter3 ) / 10 ), counter3 + 1);
   $("#quantidade1").text(counter1);
   $("#quantidade2").text(counter2);
   $("#quantidade3").text(counter3);
   if(counter1 < qntLinhas1 || counter2 < qntLinhas2 || counter3 < qntLinhas3) setTimeout("timer()", 50);
}
timer();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="quantidade1"></div>
<div id="quantidade2"></div>
<div id="quantidade3"></div>

  • Thank you and excuse the way I answered you this afternoon, I was wrong.

  • @Claydersonferreira rest assured, I think it was just a communication problem on both sides. As for my and other colleagues' tips, it’s always to help you formulate the questions to increase the chance of good answers. If you have any questions or need any adjustment, improvement or explanation, just comment here and I’ll see if I can help.

6

The body of your question expresses a different doubt than the present title. This answer refers to the title.

Is not possible, unless you rewrite the Handler for scheduling events in an engine like the Chromium, at the same time forcing the browser to behave outside the standards stipulated by Mozilla and HTML 5 specification (since all interfaces, both methods and value returns, work with unstructured milliseconds.)

Sources:

https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout#Minimummaximum_delay_and_timeout_nesting

https://html.spec.whatwg.org/multipage/webappapis.html#timers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.