実行時間の長いロジック (longLoop) を setTimeout 関数でラップしましたが、実行時間の長い実行が終了するまで UI が応答しなくなります。他の UI ボタンをクリックすることはできません。以下の例を見て、何か問題があればお知らせください。
function longLoop () {
var startLogTime, endLogTime;
startLogTime = new Date().getTime();
console.log("loop begin");
for (var i = 0; i <= 500000000; i++) {
var j = 10;
}
endLogTime = new Date().getTime();
console.log("loop execution takes " + (endLogTime - startLogTime) + " milliseconds");
}
$(document).ready(function () {
$("#btnButton").bind("click", function () {
setTimeout(longLoop, 15);
});
$("#anotherButton").bind("click", function () {
console.log("another operation clicked");
});
});
<input type="button" id="btnButton" value="Start Long Operation" />
<input type ="button" id="anotherButton" value= "Another operation" />
ありがとう