下面的代碼不會根據setInterval中設置的間隔觸發。當第一次加載頁面時返回結果集,但Ajax腳本在首次加載頁面後不運行。Ajax - setInterval not triggeredd
<html>
<head>
<script language="javascript" type="text/javascript" src="jquery.js"></script>
</head>
<body>
<!-------------------------------------------------------------------------
1) Create some html content that can be accessed by jquery
-------------------------------------------------------------------------->
<h2> Client example </h2>
<h3>Output: </h3>
<div id="output"></div>
<script id="source" language="javascript" type="text/javascript">
$(function getEvent()
{
//-----------------------------------------------------------------------
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//-----------------------------------------------------------------------
$.ajax({
\t url: 'get_events.php', data: "", dataType: 'json', success: function(rows)
\t {
\t for (var i in rows)
\t {
\t var row = rows[i];
\t var id = row[0];
\t var vname = row[1];
\t var time = row[2];
\t $('#output').append("<b>id: </b>"+id+"<b> name: </b>"+vname+"<b> time: </b>"+time)
\t .append("<hr />");
\t }
\t }
\t });
});
$(document).ready(function() {
var run = setInterval(getEvent, 1000);
});
</script>
</body>
</html>
我已經看過這個論壇的所有相近崗位,但我還是不能讓我的功能由setInterval的觸發。請參閱下面的代碼。任何幫助表示讚賞。
你不需要在'$(...)'中包裝你的函數;該語法僅用作'$(document).ready(...)'的簡寫。刪除它,它應該按預期工作。 – cmbuckley
使用瀏覽器的控制檯進行調試。沒有使用它就無法開發。 –