Tominysun

这是原始代码:

Javascript hljs
function ap_list () { var a = []; $.ajax({ url: "https://api.******.com/******/api?server=*******&type=playlist&id=**********", async :0, success: function(t) { a = JSON.parse(t); } }); $.ajax({ url: "https://api.******.com/******/api?server=*******&type=song&id=********", async :0, success: function(t) { a = a.concat(JSON.parse(t)); } }); $.ajax({ url: "https://api.*******.com/******/api?server=*******&type=song&id=*********", async :0, success: function(t) { a = a.concat(JSON.parse(t)); } }); return a; } function aplayer_fixed () { var a = new APlayer({ element: document.getElementById("aplayer-fixed"), mutex: !0, theme: "#97dffd", order: "random", lrcType: 3, fixed: !0 }); a.list.add(ap_list()); } aplayer_fixed();

由于我使用Hack方法手动拼接了一个播放列表(一个歌单+两首单曲),于是便无法使用异步ajax请求,只好把async :0设为异步请求,但是一直有一个黄色的警告⚠提醒:

Snipaste_2020-02-23_14-18-58.png

很不爽...

今天一拍脑门,突然想到了jQuerywhen().done()方法.

于是对代码稍作修改:

JavaScript hljs
(function () { var playlist=[]; $.when( $.getJSON("https://api.******.com/******/api?server=*******&type=playlist&id=**********"), $.getJSON("https://api.******.com/******/api?server=*******&type=song&id=********"), $.getJSON("https://api.******.com/******/api?server=*******&type=song&id=*********") ).then(function(a, b, c) { playlist = a[0].concat(b[0], c[0]); var a = new APlayer({ element: document.getElementById("aplayer-fixed"), mutex: !0, theme: "#97dffd", order: "random", lrcType: 3, fixed: !0 }); a.list.add(playlist); }); })();
其中为了避免playlist的变量名对全局产生污染,作了闭包处理.

目前再也没有黄色的警告⚠提醒啦!

已有 2 条评论

  1. 一新
    一新 QQ浏览器 6

    大佬都是一拍脑门~

    1. Tominysun
      Tominysun 作者君 10火狐浏览器 74

      不不不,不算大佬,只是查阅点资料,自己瞎写的...

添加新评论