-3

I would like to be able to run a javascript function that I have that auto processes it so basically runs a function every so often and I want to put that in an if statement in PHP so it will check it every so often for what it's set for. So what I mean by that is when I change a setting it will auto-update the page and either add or remove that element from the if statement. Here is the javascript code. If you need anything else let me know.

<script>
    function getStatuscall() {
        $.ajax({
            type: "GET",
            url: "<?php echo BASE_URL; ?>/actions/responderActions.php",
            data: {
                getStatuscall: 'yes',
                responder: 'yes'
            },
            success: function (response) {
                $('#getStatuscall').html(response);
                setTimeout(getStatuscall, 5000);

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log("Error");
            }

        });
    }
</script>
ankitkanojia
  • 3,072
  • 4
  • 22
  • 35
  • 1
    What is the issue with the code at the Question? – guest271314 Jan 26 '18 at 22:56
  • Please clarify your question, supply an example of what is not working that you have, and what you would like to happen. The text you have isn't very clear. Even if its a php flow with lots of comments where you need help will be useful. – IncredibleHat Jan 26 '18 at 23:00
  • if I understand you correctly, put inside ` – Jeff Jan 26 '18 at 23:05
  • update: no, I can't understand you. please use interpunctuation. – Jeff Jan 26 '18 at 23:06
  • 1
    _"when I change a setting"_? what setting? _"add or remove that element"_ - which element? – Jeff Jan 26 '18 at 23:08
  • If I understand you trying to achieve polling ? Update the page in real time ? – Tuz Jan 26 '18 at 23:08
  • 1
    _"runs a function every so often"_ - what is every so often? – Jeff Jan 26 '18 at 23:10
  • @IncredibleHat I did. I said what I wanted to be accomplished. – David Tennant Jan 26 '18 at 23:54
  • @Jeff Why does what settings I'm changing to what element I want to show or how often it runs matter? I have all of that. Also, I want the function inside of the condition. – David Tennant Jan 26 '18 at 23:54
  • @Guz Yes. The function is running at the set interval and when it is true it will show something and when it checks and it's false it goes away – David Tennant Jan 26 '18 at 23:54
  • 1
    "*I said what I wanted to be accomplished.*". Clearly you don't wish to clarify, testament by the others commenting about what it is you are trying to do, and needing clarification. If you are unwilling to help others understand your cryptic question, then others are unwilling to help you. – IncredibleHat Jan 27 '18 at 00:15
  • @DavidTennant because it would help to understand what you are triying to accomplish. And because it makes a difference if the settings change is on the server or in js. And it matters how often it runs, because either a timeout or a trigger might be the sollution. – Jeff Jan 27 '18 at 00:29
  • 1
    _"function is running at the set interval"_, _"when it checks and it's false"_,... none of this is in your question. We can't guess that. But if you don't wanna help us helping you, I'm out. – Jeff Jan 27 '18 at 00:30
  • @Jeff you don't need the time to tell me how to put it in an if condition. Since you want to be a child about it cya. – David Tennant Jan 27 '18 at 19:21

1 Answers1

0

Instead of $.ajax() you can use EventSource to maintain a persistent connection between the server and the client, to stream data to the browser.

You can use server-side logic to determine what to stream to the client. At the client you can create event listeners for specific responses from the server.

See

guest271314
  • 1
  • 15
  • 104
  • 177