我已经用 setTimeout 函数包装了长时间运行的逻辑(longLoop),但 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" />
谢谢