首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用setInterval和jQuery顺序调用数组项?

如何使用setInterval和jQuery顺序调用数组项?
EN

Stack Overflow用户
提问于 2013-10-18 06:50:37
回答 5查看 740关注 0票数 2

我有以下index.php文件:

代码语言:javascript
复制
<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文件。

代码语言:javascript
复制
<?php
//echo rand();
$questions=array(
                 "Array Item 1",
                 "Array Item 2",
                 "Array Item 3");
?>

早些时候,我调用了rand函数,在每秒钟调用该文件时,该函数每次都生成随机数。现在我已经把它评论掉了。我的要求已经改变了。现在我希望当这个文件第一次被调用时,数组项目1被回显。第二次时间阵列项目2被回响。类似的阵列项目3在第三次尝试。在此之后,setInterval不应该调用这个php文件。

我需要你的帮助。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-10-18 07:05:47

In JS:

代码语言:javascript
复制
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中的

代码语言:javascript
复制
<?php
$questions=array(
                 "Array Item 1",
                 "Array Item 2",
                 "Array Item 3");

if (isset($_POST["count"])) {
    echo $questions[intval($_POST["count"])];
}
?>
票数 3
EN

Stack Overflow用户

发布于 2013-10-18 07:22:25

以上所有的答案都应该有效,但是调用ajax 3次有什么用呢?如果您的要求是在间隔内逐个显示结果,

这是更好的解决办法,

在.php文件中

代码语言:javascript
复制
<?php
//echo rand();
$questions=array(
                 "Array Item 1",
                 "Array Item 2",
                 "Array Item 3");

echo json_encode($questions); // here is the change
?>

在.html文件中

代码语言:javascript
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2013-10-18 06:58:17

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

代码语言:javascript
复制
    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']];
    }

希望这能帮到你..。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19443332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档