Jquery刷新php和停止函数

Jquery refresh php and stop function

本文关键字:函数 刷新 php Jquery      更新时间:2023-09-26

我有一个问题。我正在刷新一个div:

<script type="text/javascript">
    <!--
    $(document).ready(function() {
        $.ajaxSetup({ cache: false }); 
        setInterval(function() {
            $('#refresh').load('call.php');
        }, 3000);
    });
    //-->
</script>
<div id="refresh"></div>

call.php:

<?php $say = 0; ?>

一切都好了。在javascript中,如果$say=1,我想停止刷新div并打开一个弹出框,echo $say;, if $say=0,我想继续刷新div,但不能这样做。有人能帮帮我吗?

编辑:我已经得到变量($say)与

var say="<?php echo $say; ?>"; 

(同页)

我有一个新问题…代码:
    var theRefresh = setInterval(function() {
        $('#refresh').load('call.php');

        checkValue();
    }, 3000);
     function checkValue(){
             var sayi = <?php echo $say; ?>;
        if(sayi==1){
           clearInterval(theRefresh);
        }
     }
});
//--></script>
在call.php:

<?php 

$say=1;
 ?>

,但javascript没有得到$say从call.php。我想从call.php文件到javascript获取值谢谢. .

创建一个函数来检查#refreshdiv中的值。假设您实际上将$say值回显到该div中。

$(document).ready(function() {
    $.ajaxSetup({ cache: false }); 
    var theRefresh = setInterval(function() {
        $('#refresh').load('call.php');
        checkValue();
    }, 3000);
     function checkValue(){
        var say = $('#refresh').html();
        if(say==1){
           clearInterval(theRefresh);
        }
     }
});

使用https://code.google.com/p/jquery-timer/

代码未经过测试

首先纠正你的php输出:

<?php echo $say = 0; ?>  

:

var timer = $.timer(function() {
    refresh();
}, 3000, true);
function refresh()
{
    $.ajax({
      url: "call.php",
      cache: false
    })
      .done(function( html ) {
        if(html == "1")
        {   
            timer.pause();      
            // here pop up code
            alert(html); 
        }
        else
        {
         $('#refresh').html(html);
         timer.play();
        }
    });
}