如何在缓存中我清楚previous变量中的Ajax调用过程中,同时发送变量到PHP变量、缓存、过程中、清楚

2023-09-10 21:58:47 作者:你的谎言我该接受还是揭穿

我无法停止previous可变装载在Ajax调用期间。

I am unable to stop the previous variable loading during the ajax call.

请找到的onclick我传递的变量和发送值Ajax调用PHP脚本。

Please find onclick I am passing the variable and sending the value to ajax call to PHP script.

我已经得到了数据:

rnc.csv

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

code

code

<?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

ajax1.php

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

当我传递价值给rnc_value.php,点击该文件中的两个值更后,比Ajax调用呼应什么都为previous值便可显示每5秒。

When I am passing the value to the rnc_value.php, after clicking the values of the file two to more , than the ajax call is echoing what ever the values having previous called displaying for every 5 seconds.

当我CONSOLE.LOG在浏览器:

When I do console.log in the browser :

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

请帮我该如何解决。

推荐答案

好了,因为我们的意见会有点长,我就会把它移到回答框。因此,这里是目前你有什么...

Alright, since our comments are going a little long, I'll move it to an answer box. So here is currently what you have...

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);
        }
    });
}

什么我的建议是:

What I'm suggesting is:

//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);
}