Подтвердить что ты не робот

Отключение сообщения long-running-script в Internet Explorer

У меня есть функция JavaScript, которая содержит цикл for, который повторяется столько раз. После вызова этой функции браузер IE отображает это сообщение:

Прекратите выполнение этого script?
A script на этой странице заставляет веб-браузер работать медленно.  Если он продолжает работать, ваш компьютер может перестать отвечать на запросы.

Как я могу это исправить? есть ли в любом случае я могу отключить это сообщение из IE?

4b9b3361

Ответ 1

Это сообщение отображается, когда Internet Explorer достигает максимального количества синхронных инструкций для фрагмента JavaScript. Максимальное значение по умолчанию - 5 000 000 инструкций, вы можете увеличить это число на одной машине с помощью редактирования реестра.

Internet Explorer теперь отслеживает общее количество выполненных операторов script и сбрасывает значение каждый раз, когда запускается новое выполнение script, например, от тайм-аута или от обработчика событий, для текущей страницы с script двигатель. Internet Explorer отображает диалоговое окно "long-running script", когда это значение превышает пороговую величину.

Единственный способ решить проблему для всех пользователей, которые могут просматривать вашу страницу, - это разбить количество итераций, которые ваш цикл выполняет с помощью таймеров, или реорганизовать ваш код, чтобы он не обрабатывал столько инструкций.

Разрыв цикла с таймерами относительно прост:

var i=0;
(function () {
    for (; i < 6000000; i++) {
        /*
            Normal processing here
        */

        // Every 100,000 iterations, take a break
        if ( i > 0 && i % 100000 == 0) {
            // Manually increment `i` because we break
            i++;
            // Set a timer for the next iteration 
            window.setTimeout(arguments.callee);
            break;
        }
    }
})();

Ответ 2

Диалоговое окно script не отвечает, когда поток javascript слишком длинный. Редактирование реестра может работать, но вам придется делать это на всех клиентских машинах. Вы можете использовать "рекурсивное закрытие" следующим образом, чтобы облегчить проблему. Это просто структура кодирования, в которой вы можете долго работать для цикла и изменять его во что-то, что делает какую-то работу, и отслеживает, где оно остановилось, уступая браузеру, а затем продолжая то, где оно остановилось, пока мы не закончим.

Рисунок 1: добавьте этот класс повторного использования Utility в ваш файл javascript. Вам не нужно будет изменять этот код:

RepeatingOperation = function(op, yieldEveryIteration) {

  //keeps count of how many times we have run heavytask() 
  //before we need to temporally check back with the browser.
  var count = 0;   

  this.step = function() {

    //Each time we run heavytask(), increment the count. When count
    //is bigger than the yieldEveryIteration limit, pass control back 
    //to browser and instruct the browser to immediately call op() so
    //we can pick up where we left off.  Repeat until we are done.
    if (++count >= yieldEveryIteration) {
      count = 0;

      //pass control back to the browser, and in 1 millisecond, 
      //have the browser call the op() function.  
      setTimeout(function() { op(); }, 1, [])

      //The following return statement halts this thread, it gives 
      //the browser a sigh of relief, your long-running javascript
      //loop has ended (even though technically we havn't yet).
      //The browser decides there is no need to alarm the user of
      //an unresponsive javascript process.
      return;
      }
    op();
  };
};

Рисунок 2, Следующий код представляет ваш код, вызывающий "прекратить выполнение этого диалога script", потому что для завершения требуется так много времени:

process10000HeavyTasks = function() {
  var len = 10000;  
  for (var i = len - 1; i >= 0; i--) {
    heavytask();   //heavytask() can be run about 20  times before
                   //an 'unresponsive script' dialog appears.
                   //If heavytask() is run more than 20 times in one
                   //javascript thread, the browser informs the user that
                   //an unresponsive script needs to be dealt with.  

                   //This is where we need to terminate this long running
                   //thread, instruct the browser not to panic on an unresponsive
                   //script, and tell it to call us right back to pick up
                   //where we left off.
  }
}

Рисунок 3. Следующий код является исправлением проблемного кода на рисунке 2. Обратите внимание, что цикл for заменяется рекурсивным замыканием, которое передает управление обратно в браузер каждые 10 итераций тяжелой маски()

process10000HeavyTasks = function() {

  var global_i = 10000; //initialize your 'for loop stepper' (i) here.

  var repeater = new this.RepeatingOperation(function() {

    heavytask();

    if (--global_i >= 0){     //Your for loop conditional goes here.
      repeater.step();        //while we still have items to process,
                              //run the next iteration of the loop.
    }
    else {
       alert("we are done");  //when this line runs, the for loop is complete.
    }
  }, 10);                   //10 means process 10 heavytask(), then
                            //yield back to the browser, and have the
                            //browser call us right back.

  repeater.step();          //this command kicks off the recursive closure.

};

Адаптировано из этого источника:

http://www.picnet.com.au/blogs/Guido/post/2010/03/04/How-to-prevent-Stop-running-this-script-message-in-browsers

Ответ 3

В моем случае во время воспроизведения видео мне нужно было каждый раз вызывать функцию currentTime для видеообновлений. Поэтому я использовал событие timeupdate для видео, и я узнал, что он был выпущен как минимум 4 раза в секунду (зависит от используемого вами браузера, см. this). Поэтому я изменил его, чтобы вызывать функцию каждую секунду следующим образом:

var currentIntTime = 0;

var someFunction = function() {
    currentIntTime++;
    // Do something here
} 
vidEl.on('timeupdate', function(){
    if(parseInt(vidEl.currentTime) > currentIntTime) {
        someFunction();
    }
});

Это уменьшает количество звонков на someFunc не менее чем на 1/3, и это может привести к тому, что ваш браузер будет вести себя нормально. Это было для меня!

Ответ 4

Я не могу прокомментировать предыдущие ответы, так как я их не пробовал. Однако я знаю, что для меня работает следующая стратегия. Это немного менее изящно, но выполняет свою работу. Это также не требует разбиения кода на куски, как, например, некоторые другие подходы. В моем случае это был не вариант, потому что у моего кода были рекурсивные вызовы на логику, которая была зациклирована; т.е. не было практического способа просто выскочить из цикла, а затем можно каким-то образом возобновить использование глобальных варов для сохранения текущего состояния, поскольку эти глобальные переменные могут быть изменены ссылками на них в последующем повторном вызове. Поэтому мне нужен был простой способ, который бы не давал шанс коду поставить под угрозу целостность состояния данных.

Предполагая, что "stop script?" диалог появляется во время выполнения цикла for() после нескольких итераций (в моем случае около 8-10), а возиться с реестром не является вариантом, здесь было исправление (для меня, во всяком случае):

var anarray = [];
var array_member = null;
var counter = 0; // Could also be initialized to the max desired value you want, if
                 // planning on counting downward.

function func_a()
{
 // some code
 // optionally, set 'counter' to some desired value.
 ...
 anarray = { populate array with objects to be processed that would have been
             processed by a for() }
 // 'anarry' is going to be reduced in size iteratively.  Therefore, if you need
 //  to maintain an orig. copy of it, create one, something like 'anarraycopy'.
 //  If you need only a shallow copy, use 'anarraycopy = anarray.slice(0);'
 //  A deep copy, depending on what kind of objects you have in the array, may be
 //  necessary.  The strategy for a deep copy will vary and is not discussed here.
 //  If you need merely to record the array orig. size, set a local or
 //  global var equal to 'anarray.length;', depending on your needs.
 // - or -
 // plan to use 'counter' as if it was 'i' in a for(), as in
 // for(i=0; i < x; i++ {...}

   ...

   // Using 50 for example only.  Could be 100, etc. Good practice is to pick something
   // other than 0 due to Javascript engine processing; a 0 value is all but useless
   // since it takes time for Javascript to do anything. 50 seems to be good value to
   // use. It could be though that what value to use does  depend on how much time it
   // takes the code in func_c() to execute, so some profiling and knowing what the 
   // most likely deployed user base is going to be using might help. At the same 
   // time, this may make no difference.  Not entirely sure myself.  Also, 
   // using "'func_b()'" instead of just "func_b()" is critical.  I've found that the
   // callback will not occur unless you have the function in single-quotes.

   setTimeout('func_b()', 50);

  //  No more code after this.  function func_a() is now done.  It important not to
  //  put any more code in after this point since setTimeout() does not act like
  //  Thread.sleep() in Java.  Processing just continues, and that is the problem
  //  you're trying to get around.

} // func_a()


function func_b()
{
 if( anarray.length == 0 )
 {
   // possibly do something here, relevant to your purposes
   return;
 }
//  -or- 
if( counter == x ) // 'x' is some value you want to go to.  It'll likely either
                   // be 0 (when counting down) or the max desired value you
                   // have for x if counting upward.
{
  // possibly do something here, relevant to your purposes
  return;
}

array_member = anarray[0];
anarray.splice(0,1); // Reduces 'anarray' by one member, the one at anarray[0].
                     // The one that was at anarray[1] is now at
                     // anarray[0] so will be used at the next iteration of func_b().

func_c();

setTimeout('func_b()', 50);

} // func_b()


function func_c()
{
  counter++; // If not using 'anarray'.  Possibly you would use
             // 'counter--' if you set 'counter' to the highest value
             // desired and are working your way backwards.

  // Here is where you have the code that would have been executed
  // in the for() loop.  Breaking out of it or doing a 'continue'
  // equivalent can be done with using 'return;' or canceling 
  // processing entirely can be done by setting a global var
  // to indicate the process is cancelled, then doing a 'return;', as in
  // 'bCancelOut = true; return;'.  Then in func_b() you would be evaluating
  // bCancelOut at the top to see if it was true.  If so, you'd just exit from
  // func_b() with a 'return;'

} // func_c()