gpt4 book ai didi

javascript - 实现另一个要运行的函数 "onSongEnd"

转载 作者:行者123 更新时间:2023-12-03 11:06:58 24 4
gpt4 key购买 nike

这里有一位好心人帮助我编写了一个脚本:

1:从我的数据库获取一些数据

2:在屏幕上显示一些信息

3:获取歌曲开始的时间,获取歌曲的持续时间(以秒为单位)并创建倒计时器来刷新页面

数据来自MySql数据库,它基本上是一个历史列表,显示当前正在播放的歌曲以及之前播放过的歌曲(元数据包含艺术家、标题、播放日期、持续时间等......

现在我的问题是:

如何添加一个函数,以便在刷新播放列表时也运行

现在我只想添加一个测试函数到 alert("extra function added"); 我希望每次 PHP 请求新数据时屏幕都会提醒此消息.

我已经尝试了很多尝试,但我总是创建函数,然后在代码的错误区域调用它们,所以我不会让你们专家感到困惑。

感谢您的阅读...如果您能提供一些线索,那将真正帮助我理解正确的程序。

这是脚本。

var PlayList = function (onUpdate, onError)
{
// store user callbacks
this.onUpdate = onUpdate;
this.onError = onError;

// setup internal event handlers
this.onSongEnd = onSongEnd.bind (this);

// allocate an Ajax handler
try
{
this.ajax = window.XMLHttpRequest
? new XMLHttpRequest()
: new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
// fatal error: could not get an Ajax handler
this.onError ("could not allocated Ajax handler");
}
this.ajax.onreadystatechange = onAjaxUpdate.bind(this);



// launch initial request

//ADDED CODE
onSongEndWrapper();
//END ADDED CODE

this.onSongEnd ();

// ------------------------------------------
// interface
// ------------------------------------------

// try another refresh in the specified amount of seconds
this.retry = function (delay)
{
setTimeout (this.onSongEnd, delay*5000);
}

// ------------------------------------------
// ancillary functions
// ------------------------------------------
// called when it's time to refresh the playlist
function onSongEnd ()
{
this.ajax.open('GET', 'playlist.php',
true);
this.ajax.send(null);
}

//ADDED CODE
function onSongEndWrapper()
{
alert("extra function added");
this.onSongEnd();
}
//END ADDED CODE



// handle Ajax request progress
function onAjaxUpdate ()
{
if (this.ajax.readyState != 4) return;
if (this.ajax.status == 200)
{
// get our response
var list = eval ('('+this.ajax.responseText+')');
// compute milliseconds remaining till the end of the current song
var start = new Date(list.dbdata[0].date_played.replace(' ', 'T')).getTime();

var now = (list.servertime);
var d = start - now + 6500
+ parseInt(list.dbdata[0].duration);

if (d < 0)

{
// no new song started, retry in 3 seconds
d = 3000;
}
else
{
// notify caller
this.onUpdate (list);
}

// schedule next refresh

//ADDED CODE
setTimeout (onSongEndWrapper.bind(this), d);
//END ADDED CODE

}
else
{
// Ajax request failed. Most likely a fatal error
this.onError ("Request failed");
}
}
}

var list = new PlayList (playlistupdate, playlisterror);

function playlistupdate (list)
{
for (var i = 0 ; i != list.dbdata.length ; i++)
{
var song = list.dbdata[i];
}
{
$("#list0artist").html(list.dbdata[0].artist);
$("#list0title").html(list.dbdata[0].title);
}
}

function playlisterror (msg)
{
// display error message
console.log ("Ajax error: "+msg);

// may want to retry, but chances of success are slim
list.retry (10); // retry in 10 seconds
}

最佳答案

创建一个包装函数。

setTimeout (onSongEndWrapper.bind(this), d);

function onSongEndWrapper()
{
alert("extra function added");
this.onSongEnd();
}

开始加载添加

document.body.addEventListener("load", startPlayList, false);
function startPlayList()
{
list = new PlayList (playlistupdate, playlisterror);
}

并更改此:

var list = eval ('('+this.ajax.responseText+')');

至:

var list = JSON.parse(this.ajax.responseText);

关于javascript - 实现另一个要运行的函数 "onSongEnd",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27807328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com