2

我有以下 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 文件。

在这里我需要你的帮助。

4

5 回答 5

3

在 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-18T07:05:47.630 回答
1

好吧,以上所有答案都应该有效,但是调用 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-18T07:22:25.143 回答
0

在第二页上,您可以使用会话

    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']];
    }

我希望这会帮助你...

于 2013-10-18T06:58:17.730 回答
0

也许你可以使用ajax:

    <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 attemp_count = 0;

            var auto_refresh = setInterval(function() {
                $.ajax({
                    url: 'xyz.php',
                    type: 'POST',
                    data: {attemp_count: attemp_count++}
                }).done(function(data) {
                        console.group('Attemp ' + attemp_count);
                        console.log(data);
                        console.groupEnd();
                        $('#mydiv').text(data);
                    });
                if(attemp_count > 2) {
                    clearInterval(auto_refresh);
                }
            }, 1000);
        });
    </script>
</head>
<body>
<div id="mydiv"></div>
</body>
</html>

和php后端文件:

<?php
//echo rand();
$questions = array(
    "Array Item 1",
    "Array Item 2",
    "Array Item 3"
);

if (array_key_exists('attemp_count', $_POST) && array_key_exists($_POST['attemp_count'], $questions)) {
    echo $questions[$_POST['attemp_count']];
}

如果您使用会话,您可能会在浏览器中遇到两个选项卡的问题。在我的情况下,你可以避免它,因为每个选项卡都将作为一个独立的应用程序运行。

于 2013-10-18T07:08:17.740 回答
0

我太慢了..但花了一些时间来处理它,所以不妨现在发布它..

<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 counter = 0;
        var auto_refresh = setInterval(function (){         
            $.post( "xyz.php", { counter: counter } ).done(function(data){
                $('#mydiv').html(data);
            });;
            counter++;
            if (counter >= 3){
                clearInterval(auto_refresh);
            }
        },1000);
    });
</script>
</head>
<body>
    <div id="mydiv"> </div>
</body>
</html>

和 xyz.php

<?php
$counter = $_POST['counter'];
$questions=array(
                 "Array Item 1",
                 "Array Item 2",
                 "Array Item 3");
echo $questions[$counter];
?>
于 2013-10-18T07:17:36.130 回答