/    Sign up×
Community /Pin to ProfileBookmark

can setTimeout() call other function within a function? or a local object’s sub-func?

hi,

see, i popup a page and simply close it immediately:

[code=html]
function doPost() {
var bump = window.open(‘some.html’,”);
bump.close();
return false;
}
[/code]

which works with no aim.. now i want to do it again with delay which means setTimeout() !!

[code=html]
function doPost() {
var bump = window.open(‘some.html’,”);
setTimeout(“bump.close()”, 3000);
return false;
}
[/code]

result is [B]Reference to undefined variable : bump[/B] because bump is not global. well i cant either make it another function and call by setTimeout() which is already in a function..

any idea ? tnx

popup page is NOT editable so no onLoad=”” as well…

who the dummy designed that setTimeout() function, idiot JS core developer !!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JPnycJan 01.2006 — Just put this statement outside the function

var bump = window.open('some.html','');


Then it'll be global and you can call the close() method
Copy linkTweet thisAlerts:
@s3prinauthorJan 02.2006 — but then bump window will be opened instantly the page loads which i dont prefer. I want to call it within the function launchs. like some form's onSubmit

and another fix solition is put it out of function as you said and close it right again which is not professional...

tnx anyway.
Copy linkTweet thisAlerts:
@s3prinauthorJan 02.2006 — Well thanks a lot [B]JPnyc[/B] !

I got the idea like ;
[code=html]
var bump;
function doPost() {
bump = window.open('some.html','');
setTimeout("bump.close()", 3000);
return false;
}
[/code]


I [B]only[/B] define [I]bump[/I] outside the function.. and assign the window.open to global var without re-defining...

so it becomes gloabal as well as called within the function. Can be closed by setTimeout() too...

thanks a lot for the IDEA buddy !! ?
Copy linkTweet thisAlerts:
@webstuffJan 02.2006 — You should try to avoid using popups as some systems and newer systems which are upto date with latest patches will block popups. Most firewalls will also block pop-ups, cookies and other methods of delivery of advertising.

IF you must advertise, your better off delivering banners to your web site page that rotate or change with eash page click, etc.

Using .setTimeout() for a task that is going to repeat, use the .setInterval() as this only needs setting once and if you use currentID=window.setInterval(myfunc,60000); will setup a reference to the event, this can be used to stop the interval with clearInterval(currentID); so you can call a routine and it will be called (in this case every 60 seconds) every 60000 milliseconds.

I built a banner script that used a system of set & clearInterval to alter the display length of the images so that slideshows can be made up where several images can be played at varying rates or held for as log as required.

clearInterval: http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/clearinterval.asp

setInterval: http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/setinterval.asp

of course, if IM barking up the wrong tree, ignor me ;o)
×

Success!

Help @s3prin 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.9,
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,
)...