javascript - Is there any way to detect the execution time of a synchronous method in js?
迷茫
迷茫 2017-06-15 09:23:26
0
5
828

Specifically, is there a method that can detect the execution time of a synchronization method? If the execution time is too long, I will take the initiative to kill this method.

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(5)
typecho

This depends on your synchronization method?

  • If it is a method provided by a third-party library, it depends on whether it provides a stop method. If not, there is nothing you can do;

  • If it involves file i/o/delete/create, you may not be able to stop it, but you can write an inverse function to restore the original state after it reports an error;

  • If this method is written by yourself and has multiple sub-functions, then you can define a timer inside the function and check whether the time exceeds the allowed value before executing each sub-function. This is a workaround;

  • If this method is written by you and has only one step, since it is a time-consuming method, then there is probably a loop, so it is okay to check the time each time it loops. . .

世界只因有你

From a JS perspective, when executing a synchronous method, the entire application will be blocked in it. At this time, even the callback triggered asynchronously by setTimeout can only be executed in the Event Loop after the execution of the synchronous method is completed, so it cannot be executed in the synchronous method. Check its own execution time and Kill itself.

From the perspective of computer science, the question asked is actually the classic halting problem:

The halting problem (English: halting problem) is a problem in computability theory in logic and mathematics. In layman's terms, the halting problem is the problem of determining whether any program can end its operation within a limited time. This problem is equivalent to the following decision problem: whether there is a program P. For any input program w, it can be judged that w will end within a limited time or loop endlessly.

This problem is NP-hard, which means that there is no algorithm with acceptable time complexity to solve it directly.

But looking beyond the academic algorithm, there are many workarounds in engineering, such as using the process management mechanism of Web Worker or PM2, etc., which will not be discussed here.

刘奇

Then you can only do multi-threading...
http://www.w3school.com.cn/ht...
However, how to kill js is also a mystery...
In the end...you can directly Wouldn’t it be better to use asynchronous||||

黄舟
console.time("time1")
$("*").each(function(){})

console.timeEnd("time1") 

I have been reminded that this is indeed not the solution. Another low-end solution may only be this:

    var func = function(t1) {
        var sum = 0,
            t2;
        for (var i = 0; i <= 10000; i++) {
            sum += i;
            t2 = new Date().getTime() - t1;
            if (t2 > 1) {
                throw '超时';
            }
        }
        console.log(sum);
    };

    try {
        func(new Date().getTime());
    } catch (e) {
        console.log(e);
    }

Not necessarily suitable for all businesses, and the dots themselves will make the process slower.

女神的闺蜜爱上我

Webworker can execute your calculation logic in another thread, the onmessage method can accept the execution results, and the terminate
method can terminate the execution of webworker

var job001DoneIndictor=false;
var myWorker = new Worker('job001.js');
//接收worker传递过来的数据
myWorker.onmessage = function(event){
    
  job001DoneIndictor=true;
};
setTimeout(function(){
    if(!job001DoneIndictor){
       myWorker.terminate();
       myWorker =null;
    }
},5000);//设置超时检测时间
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!