js非同步與解決Promise IE相容問題
function menuType(){
menuA = this;
var promise = new Promise(function(resolve, reject){
wilddog.sync().ref('').orderByKey().once('value', function (snapshot) {
var list = snapshot.val();
$("#leftNav nav").empty();
var html = "";
$.each(list, function (i, n) {
menuA.menuArr.push(i);
menuA.contentArr.push(n.total);
html += "<span class='fenlei' title='" + n.title + "' value="+i+">" + n.title + "</span>";
})
$("#leftNav nav").html(html);
resolve(menuArr);
});
});
return promise;
}
menuType().then(function(menuArr){
// 視訊查詢全部
videoAll();
});
async function videoAll() {
// 專家推薦視訊-------------------------------start
$("#expert").empty();
wilddog.sync().ref('').orderByChild('update_time').limitToLast(homepagesize).on("child_added", function (recommenddata) {
let n = recommenddata.val();
let i = recommenddata.key();
var rechtml = "";
rechtml += '<div class="flex-box" clickhref="'+ i + '" value="' + i + '"><span class="preview-box"><i class="icon icon-video-play"></i><img src="http://' + n.thumbnail + '" /></span>'+
'<h4 class="video-title">' + n.description + '</h4>'+
'<span class="corner">會員免費</span><span class="look">'+
'<i class="icon icon-eye"></i>'+ n.clickamount + '人學過</span></div>';
$("#expert").prepend(rechtml);
});
// 專家推薦視訊--------------------------------end
// 普通視訊------------------------------------start
var menulength = menuArr.length;
$(".djt").empty();
for (var j=0; j<menulength; j++)
{
// 找到分類節點名稱
mtype = menuArr[j];
var cnum = await countnum(mtype);
// 普通視訊獲取
details(mtype,cnum)
}
}
IE不支援 Promise 解決辦法(可搜尋 polyfill):
引入 <script src = "https://cdn.polyfill.io/v2/polyfill.min.js"></script>
或 <script type="text/javascript" src = "https://cdn.polyfill.io/v2/polyfill.min.js?features=es6"></script>
轉載:https://blog.csdn.net/lg632/article/details/78399732