www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] setTimeout with variable value

  1. #1
    Join Date
    Dec 2002
    Location
    Warwickshire, England
    Posts
    182

    resolved [RESOLVED] setTimeout with variable value

    Hi all,

    I need to pass the content of a PHP variable to a JavaScript setTimeout function.

    This is what I am working with:
    Code:
    function Refresh() {
    
    var rStr = "<?php echo $ScreenRefreshRate ?>";
    
    setTimeout("Refresh()", rStr );
    I don't think I am coding this correctly, can anyone advise please.

    Many thanks in advance.

  2. #2
    Join Date
    Mar 2012
    Location
    Saint-Petersburg, Russia
    Posts
    97
    Just remove quotes around php tag and make it inline:

    Code:
    var rStr = <?=$ScreenRefreshRate?>;
    Hope I write correctly though in haste and having very little skills in PHP. Sorry if something wrong %)

  3. #3
    Join Date
    Mar 2009
    Posts
    452
    Code:
    function Refresh() {
    
    var rStr = "<?php echo $ScreenRefreshRate ?>";
    
    setTimeout("Refresh()", rStr );
    }
    you were missing closing brace

  4. #4
    Join Date
    Feb 2012
    Posts
    218
    If you want this function to be calles every X miliseconds, use window.setInterval
    Code:
    function Refresh() {
      // function body
    }
    window.setInterval("Refresh()", <?php echo (int) $ScreenRefreshRate ?>) 
    // I put (int) just to make sure it's not giving JS error if you send anything else than an INT.
    @RodionGork: Try to avoid using <?=$var ?> and get used to use allways <?php echo $var ?>. There are some webservers out there that don't interpret the short form of PHP.
    Last edited by hyperionXS; 03-05-2012 at 02:22 PM.

  5. #5
    Join Date
    Dec 2002
    Location
    Warwickshire, England
    Posts
    182
    Hi all and thank you for all the replies.

    hyperionXS:

    right on the money, that works fine.

    Thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles