0

I am trying to store a variable from an ajax get request calling a PHP script. I need the variable that the information is stored in to persist within an interval function. I see the data and the call is successful on the PHP script but the variable is undefined when I go back to the code that contains the ajax request. So my question is how do I go about storing this data into a variable and making sure the variable data is retained until I leave the webpage?

index.php

// ajax repeated call on home page
<script type="text/javascript">
    var storedVariable0;
    $(document).ready(function() 
    {
        setInterval(function () 
        {
                        // ensure data was retained will be undefined first time through
            document.write(storedVariable0);

            $.ajax({
            url: '/fetchdatabase.php',
            type: 'GET',
            dataType: 'json',
            })
            .done(function(json){
            storedVariable0 = JSON.stringify(json);
            document.write(storedVariable0);
            });

                        // ensure data is retained outside of the scope of the ajax call
            document.write(storedVariable0);
        }, 1000 ); //update every second
    });
</script>

fetchdatabase.php

$sql = "SELECT content FROM messages ORDER BY id DESC LIMIT 1";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result) > 0) 
{
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) 
    {
        echo json_encode($row);
    }
}

thanks for any help

3
  • Can you elaborate on what you are trying to do with the document write? The variable, since it exists outside of the interval, and is essentially a global variable, will persist until you reload the page. Commented Jul 19, 2018 at 22:38
  • I would actually recommend against using an interval with ajax typically. As network latency and request timing is variable, it is highly likely that over time requests will start stacking, because an interval doesn't care if your asynchronous request has finished or not. Rather than using an interval, I would suggest performing a setTimeout in the done method to call the method that performs the ajax request after a second has passed. Then you still have the recursion, but the requests will not stack. Commented Jul 19, 2018 at 22:48
  • Global + ajax bugs often involve misunderstandings of the asynchronous execution of callback functions. See, e.g. stackoverflow.com/questions/40190772/… Commented Jul 19, 2018 at 22:50

1 Answer 1

0

Ajax by default is asynchronous. (synchronous is deprecating).

By asynchronous, it means that your third block of document.write(storedVariable0) will most likely be executed before the Ajax done block. So, you will get undefined before done has completed.

Doing asynchronous is recommended wau and you need change your way of coding and make sure all access to storedVatiable0 happened after done finishes.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.