我的代码有一个动画 GIF(加载图像)
我从 Ajax 调用中获取变量“数据”中的 html 页面。
$("#content").html(data); // takes about 5 seconds and the GIF animation freezes since IE is single threaded.
有没有办法使用 setTimeout / setInterval 函数逐行写入数据,以便释放线程片刻以便动画继续?
小提琴网址:http: //jsfiddle.net/deepakssn/Kp2bM/
我尝试过的替代解决方案:
<style>
#loading {
background: url(loading.gif) no-repeat center center #fff;
display: block;
position: fixed;
height: 500px;
width: 500px;
}
</style>
<script src="../scripts/jquery.js"></script>
<script>
$(window).load(function () {
function writeData(data) {
var yourLines = data.split("\n");
for (var i = 0, j = yourLines.length; i < j; i++) {
var x = setTimeout(function() {
$("#content").append(yourLines[i]);
console.log("Line No :"+[i]+" "+Date.now());
}, 5000);
}
}
function ajaxLoad() {
$.ajax({
url: "test.txt"
}).done(function (data) {
console.log("success"+Date.now());
writeData(data);
//$("#content").html(data);
console.log("loaded data"+Date.now());
});
}
ajaxLoad();
});
</script>
<div id="loading"></div>
<div id="content"></div>