var mplayer_song; function lrc_jiexi() { return new Promise(function(resolve, reject) { $.ajax({ url: '//www.toyota.com.cn/videodown/audio/toyotatimes/2024/20240325-1.lrc', dataType: 'text', success: function(data) { //setTimeout($('.open_audio').show(), 3000); data = data.replace("[00:00.00]", "[00:00.01]"); var lines = data.split('\n'); var lyrics = []; $.each(lines, function(index, line) { if (line.match(/\[\d{2}:\d{2}.\d{2,3}\]/)) { line = line.replace(/\.\d{3}(?=])/, ".50"); var time = line.match(/\[\d{2}:\d{2}.\d{2,3}\]/)[0]; var text = line.replace(time, '').trim(); lyrics.push(time + text); // 合并时间和歌词文本 } }); var lrctxt = lyrics.join('\n'); // 使用换行符连接歌词行 resolve(lrctxt); // 使用 Promise 的 resolve 方法返回结果 }, error: function(jqXHR, textStatus, errorThrown) { reject(errorThrown); // 如果 AJAX 请求失败,使用 reject 方法返回错误 } }); }); } // 使用 Promise 来处理异步结果,并赋值给 mplayer_song lrc_jiexi().then(function(lrctxt_string) { //console.log(lrctxt_string); // 这里打印从 Promise 返回的歌词文本 mplayer_song = [ { "name":"Morizo飞驰于冲绳!首届拉力挑战中充满欢声笑语", "singer":"可持续发展", "img":"/toyotatimes/imgs/2024/20240325-01/20240325-01-index.jpg", "src":"//www.toyota.com.cn/videodown/audio/toyotatimes/2024/20240325-1.mp3", "lrc": lrctxt_string // 在 Promise 解析后将歌词文本赋值给 mplayer_song } ]; setTimeout(function() { $('.open_audio').show(); }, 2000); //console.log(mplayer_song); // 在这里可以对 mplayer_song 进行其他操作,或者将其传递给其他函数 // 例如,如果您想在某个 HTML 元素中显示歌词,可以这样做: // $('.mp-lrc').text(mplayer_song[0][0].lrc); }).catch(function(error) { console.error('An error occurred:', error); // 这里处理任何错误 });