/    Sign up×
Community /Pin to ProfileBookmark

[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 );
[/code]

I don’t think I am coding this correctly, can anyone advise please.

Many thanks in advance.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@RodionGorkMar 05.2012 — Just remove quotes around php tag and make it inline:

[CODE]
var rStr = <?=$ScreenRefreshRate?>;
[/CODE]


Hope I write correctly though in haste and having very little skills in PHP. Sorry if something wrong %)
Copy linkTweet thisAlerts:
@ZABIMar 05.2012 — [CODE]function Refresh() {

var rStr = "<?php echo $ScreenRefreshRate ?>";

setTimeout("Refresh()", rStr );
}[/CODE]


you were missing closing brace
Copy linkTweet thisAlerts:
@hyperionXSMar 05.2012 — 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.
[/CODE]


@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.
Copy linkTweet thisAlerts:
@dcjonesauthorMar 05.2012 — Hi all and thank you for all the replies.

hyperionXS:

right on the money, that works fine.

Thank you.
×

Success!

Help @dcjones spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...