简体   繁体   English

我的一组函数执行完后该怎么办?

[英]How to do something after my set of functions has been executed?

I have a set of functions, and I want to do something, like show a loading animation, while these functions are running! 我有一组函数,我想做一些事情,例如在运行这些函数的同时显示加载动画! and when they stop i want to show an alert, for example. 例如,当他们停止时,我想显示一个警报。 All this on jQuery. 所有这些都在jQuery上。

Anyone has a tip? 有人给小费吗?

Some abstract code: 一些抽象代码:

//these are my functions:
function a(number){number=number*2};
a(1); a(2); a(3);
//While this functions are running i want to show a gif, for example!
//When the three callings this function has done, i want to show an alert

Your JavaScript is executed in the same single thread as all other rendering within the document and it is therefore pointless to make a UI change before running a lot of synchronous JS (your functions) only to then reverse the UI change. 您的JavaScript与文档中的所有其他呈现都在同一单线程中执行,因此在运行大量同步JS(您的函数)之前先进行UI更改是毫无意义的,而仅是撤消UI更改即可。 The UI will remain the same to the user. 用户界面将保持不变。

Doing what you want would only be useful in situations where asynchronous requests are occurring, and in such situations you would have to rely on callback functions. 进行所需的操作仅在发生异步请求的情况下有用,并且在这种情况下,您将不得不依赖回调函数。

Like this: 像这样:

// code to show loading message

function a(number){number=number*2};
a(1); a(2); a(3);

// code to hide loading message

The code is executed from top to bottom, so by the time function are running, the loading message will be showing because it is before those functions and when functions are done, it will hide again. 代码是从上到下执行的,因此,在功能运行时,将显示加载消息,因为该消息位于这些函数之前,并且在完成这些功能后,它将再次隐藏。

just before the call to the function display some animation gif on the screen 在调用该函数之前,在屏幕上显示一些动画gif

and just after the call the function remove it and show your alert. 然后在调用该函数后将其删除并显示您的警报。

thats it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM