如何在 Ajax 调用期间清除缓存中的先前变量,同时将变量发送到 PHP

How do I clear previous variable in cache during the Ajax call while sending variable to PHP

我无法在 ajax 调用期间停止之前的变量加载。

请找到 onclick 我正在传递变量并将值发送到 ajax 调用 PHP 脚本。

我有数据:

rnc.csv

DLRNC01
DLRNC02
DLRNC03
DLRNC04
DLRNC05
DLRNC06
DLRNC07
DLRNC08
DLRNC09
DLRNC10
DLRNC11
DLRNC12
DLRNC13
DLRNC14

代码

<?php

    if (($handle = fopen("rnc.csv", "r")) !== FALSE) {
        $i=0;
        $values=array();
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

?>

<a href="javascript:void(0)"  onclick="pop(<?php echo "'".$data[0]."'"; ?>)" > <?php echo $data[0]; ?></a></br>
<?php               
    }
    fclose($handle);
    }
    else{
        echo "File not found";
    }
?>

<div id="container1"></div>

<script>

//even I cleared the chache using the cache:false

//for every click different variable is passing to the ajax call and than to PHP script
        function pop(rnc)
        {
            $.ajax({
            url: 'ajax1.php',
            type: "POST",
            cache: false,
            //dataType:'json',
            data:{rnc:rnc},
            success: function(data){

                        $("#container1").html(data);

                        setInterval(function(){

                                console.log(rnc);

                                $("#container1").html(data);

                        },2000);
                }
            });
        }

</script>

ajax1.php

<?php echo $_POST['rnc']; ?>

当我将值传递给 rnc_value.php 时,在单击文件的值两次或更多之后,ajax 调用回显了之前调用的每个值的显示5 秒。

当我在浏览器中执行 console.log 时:

ajax.php:344 DLRNC01
ajax.php:344 DLRNC05
ajax.php:344 DLRNC07
ajax.php:344 DLRNC04
ajax.php:344 DLRNC01
ajax.php:344 DLRNC02
2 ajax.php:344 DLRNC05
ajax.php:344 DLRNC06
ajax.php:344 DLRNC07
ajax.php:344 DLRNC04
ajax.php:344 DLRNC01
ajax.php:344 DLRNC02

请帮我解决一下..

好的,由于我们的评论有点长,我将其移至答案框。所以这是你目前拥有的...

function pop(rnc) {
    $.ajax({
        url: 'ajax1.php',
        type: "POST",
        cache: false,
        data:{rnc:rnc},
        success: function(data){
            $("#container1").html(data);

            setInterval(function() {
                console.log(rnc);
                $("#container1").html(data);
            },2000);
        }
    });
}

我的建议是:

//storage for the interval that will persist beyond the function call
var popInterval = null;
function pop(rnc) {
    //if it is not the first invocation of pop, need to kill the previous interval
    if (popInterval !== null) {
        clearInterval(popInterval);
    }

    //start the new interval with the new 'rnc' data provided and store the interval reference
    popInterval = setInterval(function() {
        $.ajax({
            url: 'ajax1.php',
            type: "POST",
            cache: false,
            data:{rnc:rnc},
            success: function(data){
                console.log(rnc);
                $("#container1").html(data);
            }
        });
    },2000);
}