Wrox Programmer Forums
|
BOOK: Beginning JavaScript
This is the forum to discuss the Wrox book Beginning JavaScript by Paul Wilton; ISBN: 9780764544057
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 20th, 2008, 01:20 PM
Registered User
 
Join Date: Jun 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default SetTimeout() not always working

I am using the Set Timeout function to set my focus on a textbox in a modal popup when it is displayed. THe problem I am running into is that sometimes it is working and other times it does not work. I can'd figure out the reason why. It is set up on the client click of an image button. Can anyone assist me with having it work all the time? Below is my code.
Code:
function SetFocus(sender)
    {
        setTimeout("document.getElementById('<%= txtLName.ClientID %>').focus()", 1000); 
    }
Thanks in advance!

~Candi
 
Old June 20th, 2008, 01:56 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I can think of a couple of reasons...

(1) Somehow, you aren't really getting the correct ID of the target. To check this, do a VIEW-->>SOURCE of the HTML.

(2) If it takes longer than one second for the modal dialog to completely load, then *of course* this will fail! There is no object there to set the focus *to*.

Number (2) is easy to fix:

function SetFocus( )
{
    var obj = document.getElementById('<%= txtLName.ClientID %>');
    if ( obj != null )
    {
        obj.focus( );
        return;
    }
    setTimeout("SetFocus()", 100);
}

You just keep waiting, 100 milliseconds at a time, until the object *DOES* exist.
 
Old June 20th, 2008, 02:47 PM
Registered User
 
Join Date: Jun 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried what you posted, Old Pedant. I even tried a few variations of it, but it would not work at all. I started to get the error that focus can not be set if the control is invisible.....etc. I looked at the View Source, and the correct Client ID is being passed in.

~Candi
 
Old June 20th, 2008, 03:10 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Ahhh...not what I thought..."the error that focus can not be set if the control is invisible..."

Interesting.

Maybe put the focus() call in a try...catch and retry if the error is caught??

Won't work on older browsers without try...catch, but you can't have everything.

Untested, but something like...
Code:
function SetFocus( )
{
    var obj = document.getElementById('<%= txtLName.ClientID %>');
    if ( obj != null )
    {
        try { 
            obj.focus( );
            return; // done if successful
        } catch (ignored) {
            /* just fall through */
        }
    }
    setTimeout("SetFocus()", 100);
}
I guess you don't really even need it that complex. Could probably simply do it as

Code:
function SetFocus( )
{
    try {
        document.getElementById('<%= txtLName.ClientID %>').focus();
        return;
    } catch (ignored) {
        /* just fall through */
    }
    setTimeout("SetFocus()", 100);
}
No?
 
Old June 20th, 2008, 03:24 PM
Registered User
 
Join Date: Jun 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Old Pedant,

Although this time it did not break when executed, it also did not work. Again I tried some variations, but nothing. I also checked the Source to make sure it was the right ClientID still and it is the right onw.

~Candi
 
Old June 20th, 2008, 03:26 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Sorry, then. Out of ideas.

It must be a timing issue, if it sometimes works and sometimes doesn't. But a timing of what I'll be darned if I know.
 
Old June 20th, 2008, 03:29 PM
Registered User
 
Join Date: Jun 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Old Pedant,
Thanks for trying to help! Hopefully someone will see this and be able to help me so we can both have bigger brains. :D

~Candi
 
Old June 20th, 2008, 03:29 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

If you want to post real (minimal, please!) code, I'll try playing with it, but no guarantees, at all.
 
Old June 20th, 2008, 03:30 PM
Registered User
 
Join Date: Jun 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I posted what I had. The function is called by the client click of an image button.

~Candi
 
Old June 20th, 2008, 03:50 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Ummm...don't you *ALSO* cause the modal dialog to show??? That's the part I meant. What's the dialog look like? So we don't go create something entirely different. Just the HTML, mind you. Not the ASP.NET code.





Similar Threads
Thread Thread Starter Forum Replies Last Post
getting a return value from setTimeout() peirix Javascript 9 November 28th, 2008 06:22 PM
setTimeOut with a zero parameter? calgarychinese ASP.NET 1.0 and 1.1 Basics 3 April 5th, 2007 08:44 PM
setTimeout second to Minute mateenmohd Javascript 6 November 28th, 2004 02:12 AM
Implementing setTimeout causing problems sherbir Javascript 1 August 12th, 2004 09:47 AM
Using setTimeout PortGuy Javascript 7 March 25th, 2004 02:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.