How to prevent “Stop running this Script” in browsers?

后端 未结 3 1485
闹比i
闹比i 2021-02-15 12:49

I have an ASP.NET MVC page that has JQuery Editable Datatable that has 14 columns. I have a button (Apply No Findings)to do Client side calcultions snd apply for all Rows in tha

相关标签:
3条回答
  • 2021-02-15 13:11

    You can also consider HTML 5 workers

    A web worker is a JavaScript running in the background, without affecting the performance of the page. When executing scripts in an HTML page, the page becomes unresponsive until the script is finished. A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page.

    Creating a new worker is simple. All you need to do is call the Worker() constructor, specifying the URI of a script to execute in the worker thread, set the worker's Worker.onmessage property to an appropriate event handler function.

    var myWorker = new Worker("my_task.js");
    
    myWorker.onmessage = function (oEvent) {
      console.log("Called back by the worker!\n");
    };
    

    Alternatively, you could use addEventListener() :

    var myWorker = new Worker("my_task.js");
    
    myWorker.addEventListener("message", function (oEvent) {
      console.log("Called back by the worker!\n");
    }, false);
    
    myWorker.postMessage(""); // start the worker.
    

    You can see more details at: https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers

    0 讨论(0)
  • 2021-02-15 13:23

    The problem is javascript is single-threaded, so if there is a function that takes too long to complete, this function could cause the UI not responding. Therefore, the browser will warn the user about long running script by displaying the message: "Stop running this script". The solutions to this problem are:

    • Optimize your code so the function does not take so long.
    • Use setTimeout to break the function execution into many pieces that are short enough.

    Example code pattern:

    var array = []; //assume that this is a very big array
    var divideInto = 4;
    var chunkSize = rowCount/divideInto;
    var iteration = 0;
    
    setTimeout(function doStuff(){
      var base = chunkSize * iteration;
      var To = Math.min(base+chunkSize,array.length);
      while (base < To ){
          //process array[base]
          base++;
      }
      iteration++;
      if (iteration < divideInto)
          setTimeout(doStuff,0); //schedule for next phase
    },0);
    

    The solution you take in your Example(2) is correct, but there is a problem in your code. That's the setTimeout does not run. Try changing your code like this:

    RepeatingOperation = function(op, yieldEveryIteration) 
      {  
      var count = 0;  
      var instance = this;  
      this.step = function(args) 
        {    
           op(args); 
           if (++count <= yieldEveryIteration) 
           {          
             setTimeout(function() { instance.step(args); }, 1, [])         
           }    
        };   
      };
    

    Modify your function ApplyNoFindings(), try this:

    if (++i > iTotalRecords) 
     { 
        UpdateSummaryLine();
        UpdateSummaryLineReasonCode();
     }  
    

    instead of:

    if (++i < iTotalRecords) 
         { 
            ro.step(); 
         }  
         else 
         { 
            UpdateSummaryLine();
            UpdateSummaryLineReasonCode();
         }  
    

    Note: not tested, just give you an idea how it should work

    0 讨论(0)
  • 2021-02-15 13:37

    Despite another answer already accepted I want to place here general information for developers who will have this problem in feature:

    By this link you can find information how to fix this from user side http://support.microsoft.com/kb/175500 - user can add one Registry key.

    Also you can find there information about when this "Running slow" message appears:

    By default, the key does not exist. If the key has not been added, the default threshold limit for the time-out dialog box is 5,000,000 statements for Internet Explorer 4 and later versions.

    As you see slowness measured in javascript statements, not in time.

    0 讨论(0)
提交回复
热议问题