0
我的JavaScript有一些問題。此腳本使用Ajax GET從PHP頁面檢索內容。並根據名爲count
的變量獲取內容。JavaScript - 變量不重置
此代碼一直運行,直到它到達我數據庫中的最後一項。當它從PHP頁面獲得no_data
時:它應該重置counter
變量並從頂部開始,然後繼續無限循環。但是,當我嘗試重置與count = 0;
變量:沒有任何反應。這是爲什麼?
這裏是我的JS代碼:
var count = 0;
var timer;
$(window).load(function() {
load_new_entry();
// timer = window.setInterval(load_new_entry, 7500);
// window.clearTimeout(timer);
});
function load_new_entry() {
fade_out();
}
function fade_out() {
$(".container").fadeOut(1500, load_data);
window.clearTimeout(timer);
}
function fade_in() {
$(".container").fadeIn(1500);
timer = window.setInterval(load_new_entry, 7500);
}
function load_data() {
getDetails(count);
count++;
}
function getDetails(count) {
$.get("data.php?count="+count,function(data,status){
if (data != 'no_data') {
var jsonReturnData = $.parseJSON(data);
$(".container").html(jsonReturnData.content);
fade_in();
//console.log("Load details: " + count);
} else {
count = 1;
fade_in();
//console.log("Counter Reset: " + count);
}
});
}
這裏是我的PHP:
<?php
mysql_connect("127.0.0.1", "root", "");
mysql_select_db("infoscreen");
function grab_data($count) {
$get_data = mysql_query("SELECT * FROM display ORDER BY `id` ASC LIMIT ".$count.",1");
while ($data = mysql_fetch_assoc($get_data)) {
$id = $data['id'];
$content = $data['content'];
$return_data = array('id' => $id, 'content' => $content);
//echo $return_data['content'];
echo json_encode($return_data);
}
}
if (isset($_GET['count'])) {
$count = $_GET['count'];
$utf_8 = mysql_query("SET CHARACTER SET utf8");
$data_exists = mysql_query("SELECT * FROM display ORDER BY id ASC");
if (mysql_num_rows($data_exists) > $count) {
// Next record is available
grab_data($count);
} else {
// Next record is not available
echo "no_data";
}
}
不要使用'count'作爲參數。用getDetails()替換'getDetails(count)'' – Satpal
@Satpal哦,我傻!修復!將其張貼爲答案,並接受它。所以你得到你的積分 –
哦,我剛剛看到它,標記他的答案是正確的,當他發佈... – reyaner