/    Sign up×
Community /Pin to ProfileBookmark

passing javascript function variables

I have a timer script that I am trying to alter to be more functional for my website.

I am trying to pass a variable (php page location) to the timer so that when it times out it will load a certain page. This page will vary depending on why the timer was initiated.

here is the code:

[CODE]<SCRIPT LANGUAGE = “JavaScript”>
<!–
//var page = ‘../index.php’
var secs
var timerID = null
var timerRunning = false
var delay = 1000

function InitializeTimer(page)
{
// Set the length of the timer, in seconds
secs = 10
StopTheClock()
StartTheTimer(page)
}

function StopTheClock()
{
if(timerRunning)
clearTimeout(timerID)
timerRunning = false
}

function StartTheTimer(page)
{
if (secs==0)
{
StopTheClock()
window.open(page, ‘_self’)
// Here’s where you put something useful that’s
// supposed to happen after the allotted time.
// For example, you could display a message:
}
else
{
secs = secs – 1
timerRunning = true
timerID = self.setTimeout(“StartTheTimer()”, delay)
}
}
//–>
</SCRIPT>[/CODE]

here is the code to execute the script:

[CODE]
<script language=”javascript”>InitializeTimer(“../index.php”)</script>[/CODE]

with this the timer still works and tries to got to the new page but the “page cannot be found” error comes up and in the address field shows “undefined” at the end of my url address.

Can anyone tell me what I am doing wrong. Is there a problem with the type of characters that I can pass to the function? Can I carry a variable through 2 functions as I have in the above code?

Cheers

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsSep 17.2004 — you are not passing 'page' to the function StartTheTimer(page) with

timerID = self.setTimeout("StartTheTimer()", delay)


easy way insert

Page=page;

in function InitializeTimer(page)

and

window.open(Page, '_self')
in

function StartTheTimer()

ps

passing variables between functions is not normally a problem but as you are using a Timeout the internal variable 'page' will not exist when the call is made. The variable must therefore be converted to a global 'Page'

Hope this helps
×

Success!

Help @heals1ic 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.25,
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,
)...