我有一个显示随机文本的PHP脚本:
<?php
$quotes = array(
'quote 1',
'quote 2',
'quote 3',
'quote 4',
'quote 5',
);
$i = array_rand($quotes);
header('Content-Type: text/plain; charset=utf-8');
print($quotes[$i]);
?>
我怎么能每分钟都在我的网页(它本身就是Facebook上的一个iframe应用程序)上显示一句新的引号(UTF8编码的俄语文本)呢?
我需要在这里使用jQuery吗?或者有一个更轻松的解决方案,可以在普通浏览器中工作吗?
我还没有任何AJAX的经验。
更新:
正如下面Uku Loskit所建议的,我添加了以下片段,效果很好。有人能告诉我如何用fadeIn()把新引号淡入吗?
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
setTimeout( function() {
$.get("/hint.php", function(data) {
$("#hint").html(data);
});
}, 5*60*1000 );
});
</script>
谢谢!Alex
使用setTimeout()方法。
我想说,使用jQuery(尤其是缩小版)"只用于AJAX"并没有什么大不了的,因为它提供了一个跨浏览器兼容的解决方案,而且编程起来更容易。
示例:
function getNewQuotes() {
$.get("random_quotes.php", function(data) {
// set the response from random_quotes.php to this div
$("#quotesDiv").html(data);
});
}
// 60000 milliseconds = 60 seconds = 1 minute
var t=setTimeout("getNewQuotes()", 60000);
至于混合"非JQuery和Javascript"的问题,据我所知,没有JQuery函数,总而言之,JQuery仍然是Javascript,不需要一直依赖JQuery特定的代码,但只对一致性有用。
编辑:
$(function() {
setTimeout( function() {
$.get("/hint.php", function(data) {
// first hide, then insert contents
$("#hint").hide();
$("#hint").html(data);
// you can probably chain this together into one command as well
$("#hint").fadeIn("slow");
});
}, 5*60*1000 );
});
您不需要jquery,但它更容易。只需从手册中学习如何获得一次的基本示例:
$.ajax({
url: "yourFileName.php",
context: document.body,
success: function(result){
$('#someDiv').html('result');
}
});
然后添加javascript代码来定期执行此操作。我相信你也可以从jquery中找到一些漂亮的东西:)