0

I have an ajax application using PHP. It loads videos, and ajax needs to load another video after a variable number of seconds it gets from the database. I am trying to echo a setTimeout along with the video, like so, with loadContent being the function that loads videos.

            echo '<iframe id="youtubeFrame" src="//www.youtube.com/embed/
            '.$row['youtube'].'
            ?autoplay=1" frameborder="0" allowfullscreen></iframe>';

            $refreshTimer = $row['end'] - $time;
            $refreshTimer = $refreshTimer * 1000;
            echo $refreshTimer;
            echo '<script>
            setTimeout(loadContent, '.$refreshTimer.');
            </script>';

The video loads, the variable refreshTimer is the right amount of seconds, but it won't run the setTimeout.

How do I get the page to refresh when my database tells it to?

UPDATE: I've been told the variable needs to be turned into an Int, but that can't be the case, because when there is no video, this code runs.

        echo '<h1>Upload content to start the show</h1>';

            echo '<script>
            setTimeout(loadContent, 4000);
            </script>';

This doesn't involve any php variables, it is a direct number, and this doesn't work as well. I'm curious on how to pass data to ajax through the success, but I'm having trouble looking up relevant information online when searching.

10
  • Why not setting the timeout event on AJAX success ? Commented Nov 20, 2014 at 6:31
  • What does echo $refreshTimer show ? And does loadContent get called at least once? Wondering if your confusing this with setInterval. Commented Nov 20, 2014 at 6:32
  • $refreshTimer shows the amount I want it to, and loadContent is getting it's initial call. As to Syed Quaib, that's a great point. I haven't had to use success yet. How do I pass data to it? Commented Nov 20, 2014 at 6:34
  • You still ain't told me what $refreshTimer holds. And is it string or int data type? If you don't convert it to a number it won't work. My guess is javascript will intepret the php var as a string. So try to use parseInt Commented Nov 20, 2014 at 6:36
  • Ah, I think you're right Dave. Is one method preferable over the other? Commented Nov 20, 2014 at 6:38

1 Answer 1

1

My best guess based on your information provided is the PHP variable will be considered a string and not an integer.

So convert it like this:

echo '<script>
 setTimeout(function(){loadContent();}, parseInt('.$refreshTimer.'));
</script>';

Side note it would be wise to learn how to seperate PHP and JS (server side and client side) using ajax in the future :)

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

6 Comments

You didn't show the parseInt. Also, can I run a javascript function on a php variable. I thought parseInt was suppose to be used with javascript functions.
@Goose I have edited it in now. PHP is a totally different language to JS, theres no way JS will know if the variable is holding a number or a string. It will usually default to string until you convert it to integer. And setTimeout expects a number or it won't work.
Ah, this won't work. I updated my question to show another crucial hint.
@Goose i don't see any thing different?
@Goose try my updated version which holds an unnamed function. It might solve it - not sure why it would if the parseInt does not.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.