How to manage Jquery processing or pure Javascript?

Asked

Viewed 112 times

-3

I recently had the following problem in a hybrid application: A command was not executed because another process was faster, and redirected the page. Not knowing how the javascript process management works (language that was not originally meant to behave like a traditional programming language) I used the setIntervel and setTimeOut functions to improvise a loading "queue", administered in milliseconds. That was the solution to that particular case, but it is far from ideal according to everything I have seen of development. I’ve never heard of programming treats in javascript, or memory management (except for the scope of var/Let variables), and to contextualize this ignorance of mine, I don’t consider myself a javascript developer, so the doubt. By coincidence, I read in a material about jquery, that the Jquery script runs when the whole page is loaded, which made me think that maybe I have an alternative to that question. If the option is developer directly to a server in nodejs, I find it unviable, because I apply javascript in "n" contexts outside of server nodejs. Note that the doubt is not exclusively about performance, more manipulating processes, where execution 1 necessarily happens before execution 2, without cluttering the code with thousands of "if"s or "dowhile".

function primeiro(){
    //Esse código acontece em 500 milissegundos 
}

function segundo(){
    //Esse código acontece em 200 milissegundos 
}

primeiro();
segundo();
  • 1

    " that the Jquery script runs when the whole page is loaded" this is simply the event DOMContentLoaded&#I suggest taking a look at the events available for window and document.

  • 3

    Sérgio Lima: how does the answer answer answer your question? I don’t see the relationship between the two...

  • 1

    Also did not understand the relationship of the question with the answer accepted. I understood that you want a way to ensure that segundo() will only be executed after primeiro() finish, regardless of how long it takes - and in that case, I suggest taking a look at the Promises. performance.now() is only measuring the time it took something to perform, IE, the functions will already have run and you did not guarantee any order...

  • @Sergio He wanted to know what is the value of performance,he said it was right and said it solved. I’m confused...

  • I just found the question’s relationship with my answer strange. I even made an answer, but he said it was wrong (about performance).

  • I will create a different response version, to make it clear at what point your problem solves, and the traditional timers (setInterval and setTimeOut) do not solve.

  • 1

    Sérgio, it would also be interesting [Dit] the question to clarify what the problem is, so the answer starts to make more sense (because the way it is, they do not seem to have any apparent relationship).

Show 2 more comments

1 answer

-5


Use this method to calculate the speed of your code:

function doSomething() {
  for (var i = 0; i < 10; i++) {
    window.document.getElementById('resultado').innerHTML = i;
  }
}
var t0 = performance.now();
doSomething();
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.");
<DOCTYPE html>
<html>
<body>
<p id='resultado'></p>
</body>
</html>

Browser other questions tagged

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