我有以下index.php文件:
<html lang="en">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var auto_refresh = setInterval(function (){
//alert("abc");
$('#mydiv').load('xyz.php').fadeIn("slow");
}, 1000);
});
</script>
</head>
<body>
<div id="mydiv"> </div>
</body>
</html>在上面的文件中,我试图在1秒后调用我的xyz.php文件。工作很好。下面是我的xyz.php文件。
<?php
//echo rand();
$questions=array(
"Array Item 1",
"Array Item 2",
"Array Item 3");
?>早些时候,我调用了rand函数,在每秒钟调用该文件时,该函数每次都生成随机数。现在我已经把它评论掉了。我的要求已经改变了。现在我希望当这个文件第一次被调用时,数组项目1被回显。第二次时间阵列项目2被回响。类似的阵列项目3在第三次尝试。在此之后,setInterval不应该调用这个php文件。
我需要你的帮助。
发布于 2013-10-18 07:05:47
In JS:
var count = 0;
$(document).ready(function(){
var auto_refresh = setInterval(function (){
$('#mydiv').load('xyz.php', {count: count}, function () {
count = count + 1;
//after three attempts it won't call php file.
if (count > 2) {
clearInterval(auto_refresh);
}
}).fadeIn("slow");
}, 1000);
});PHP中的:
<?php
$questions=array(
"Array Item 1",
"Array Item 2",
"Array Item 3");
if (isset($_POST["count"])) {
echo $questions[intval($_POST["count"])];
}
?>发布于 2013-10-18 07:22:25
以上所有的答案都应该有效,但是调用ajax 3次有什么用呢?如果您的要求是在间隔内逐个显示结果,
这是更好的解决办法,
在.php文件中
<?php
//echo rand();
$questions=array(
"Array Item 1",
"Array Item 2",
"Array Item 3");
echo json_encode($questions); // here is the change
?>在.html文件中
<html lang="en">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.getJSON( "xyz.php", function( data ) {
// using $.getJSON rather of $.load
// note that you can use any of jquery ajax method. with required changes,
$('#mydiv').fadeIn("slow");
$.each( data, function( key, val ) {
setInterval(function (){
$('#mydiv').append( val + "<br>"); // or whatever is your formatting
});
});
});
});
</script>
</head>
<body>
<div id="mydiv"> </div>
</body>
</html>发布于 2013-10-18 06:58:17
在第二页上,您可以使用会话。
session_start();
if(!isset($_SESSION['count'])){
$_SESSION['count']=0;
}
else
{
$_SESSION['count'] = $_SESSION['count'] +1;
}
$questions=array(
"Array Item 1",
"Array Item 2",
"Array Item 3");
$qcount=count($question);
if($qcount< $_SESSION['count']){
// NA
}
else{
echo $question[$_SESSION['count']];
}希望这能帮到你..。
https://stackoverflow.com/questions/19443332
复制相似问题