Диалоговое окно не отвечающего скрипта показывает, когда какой-то поток JavaScript занимает слишком много времени или слишком завершен.Редактирование реестра может работать, но вам придется делать это на всех клиентских компьютерах.Вы можете использовать «рекурсивное закрытие» следующим образом, чтобы облегчить проблему.Это просто структура кодирования, в которой вы можете долго выполнять цикл for и превращать его во что-то, что выполняет некоторую работу, и отслеживает, где он остановился, уступая браузеру, затем продолжая с того места, где он остановился, до тех пор, пока мы не закончим.
Рисунок 1. Добавьте этот класс утилит RepeatingOperation в ваш файл 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, Следующий код представляет ваш код, который вызывает диалоговое окно «Остановить выполнение этого сценария», потому что это занимает много времени длязавершено:
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