您可以通過JQuery將一個活動表輪詢到一個從活動表讀取的PHP腳本,並通過這樣做來檢測用戶是否按下了按鈕。一個簡單的方法是這樣的: 用戶按下一個按鈕 - >您將活動以時間戳發送到活動表,以便知道用戶何時按下了按鈕(我們稍後會介紹這一點)。 你可以通過JQuery發送一條信息給PHP,然後將它插入數據庫。 活動表將包含: id(INT),primary,auto_increment。 button(INT)[點擊的按鈕ID] dateposted [timestamp] 現在,每次您使用JQuery和PHP調用該表/調用該表時,都會查詢時間(重要)和按鈕ID ,這是我經常做像這樣:
客戶端:(你可以有一個不同的ID爲像這樣每個按鈕)
var auto_refresh = setInterval(
function reloadstring()
{
$.get("checknewactivity.php", function(buttonid){
if (buttonid == 1) /* Say he clicked the record button for example */
{
$("#recordbtn").css({"background-color":"#FF0000"}); /* Make the record button red */
}
});
}, 1000); // refresh every 1000 milliseconds
/* And this would poll every 1 second as it says above */
服務器端:(checkactivity.php)
<?php
checkactivity();
function checkactivity() {
$querystats = "SELECT activity.id, activity.buttonid, activity.dateposted FROM activity ORDER BY id DESC LIMIT 1";
$resultstats = mysqli_query($yourdbhandle,$querystats);
$num_stats = mysqli_num_rows($resultstats);
$rowactivity = mysqli_fetch_assoc($resultstats);
if($num_stats > 0) { /* If there was activity */
$activity_id = $rowactivity["id"];
$activity_date = $rowactivity["dateposted"]; //I can't indent this here for some reason.
$activity_buttonid = $rowactivity["buttonid"];
$timeactivity = strtotime("$activity_date");
$actualtime = time(); //The actual time in a timestamp
$timetoseconds = $actualtime - $timeactivity; //Actual time - timeactivity
if($timetoseconds < 6) { /* If the last activity was sent less than 6 seconds ago */
echo "$activity_buttonid"; //This would send the last clicked button id
}
}
}
?>
這不是b最簡單的方法,但最好的方法是使用webSockets或node.JS來避免一遍又一遍地輪詢數據庫,一旦服務器發現更新,只發送結果給客戶端。這是一份草稿,並簡要說明了如何做到這一點。
這段文字很難看懂 – 2013-04-06 22:43:28
ok我編輯過。希望這樣做更有意義:P – 2013-04-06 22:45:05
您的目標是爲此編寫軟件作爲學習練習或解決軟件問題?如果你只需要一個工具來溝通,我建議一個共享的谷歌文檔或trello板來實現這一點。如果你想學習如何編寫軟件,我會從CodeAcademy開始,並解決這樣的問題。 – mconlin 2013-04-06 23:51:01