gpt4 book ai didi

php - AJAX:每分钟显示一条随机线

转载 作者:行者123 更新时间:2023-11-30 09:04:13 27 4
gpt4 key购买 nike

我有a PHP-script显示随机文本:

<?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 上是 an iframe app)上显示新报价(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>

谢谢!亚历克斯

最佳答案

使用setTimeout()方法。

我想说“只为 AJAX”使用 jQuery(尤其是缩小版本)并没有什么繁重的事情,因为它提供了一个跨浏览器兼容的解决方案并且更容易编程。

例子:

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 );
});

关于php - AJAX:每分钟显示一条随机线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6483140/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com