![](/img/trans.png)
[英]How to run a funtion onclick of button from one component and funtion run on another compoenent in react native
[英]Implementing another funtion to be run “onSongEnd”
一位好心人幫助我整理了一個劇本,該劇本:
1:從數據庫中獲取一些數據
2:在屏幕上顯示一些信息
3:獲取開始歌曲的時間,獲取歌曲的持續時間(以秒為單位),並創建一個倒數計時器以刷新頁面
數據來自MySql數據庫,它基本上是一個歷史列表,該列表顯示當前正在播放的歌曲以及之前播放的歌曲(元數據包含藝術家,標題,播放的日期,持續時間等內容的概要信息...
現在我的問題是:
當我需要刷新播放列表時,如何添加要運行的功能
現在,我只想向alert("extra function added");
添加一個測試功能alert("extra function added");
添加了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();
}
要開始onload添加
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.