Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Aug 2010
    Location
    Seattle
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    setTimer() in link

    I can't figure out why this isn't working.

    I'm calling a function from a link in an iFrame, but I want a second function (which builds upon the first) to be called a second after the first one. So I have this:

    <a href="javascriptarent.func1();setTimeout('parent.func2', 1000)">Click here</a>

    setTimeout works when it's the only function I'm calling, but it appears that having the two together erases the delayed function.

    Thoughts?
    ~gyz

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    if parent.func1() redirects to another page, then your code wont work.

  • #3
    New Coder
    Join Date
    Aug 2010
    Location
    Seattle
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nope, stays on the same page, but both Func1 and Func2 assign new values to the same array, then both call a third function.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    This is wrong.

    Code:
    setTimeout('parent.func2', 1000)
    The correct code is
    Code:
    setTimeout(parent.func2, 1000)
    without the quotes/apostrophes.

    You *can* code it as
    Code:
    setTimeout('parent.func2()', 1000)
    but there's no point. Keep it short and simple.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Aug 2010
    Location
    Seattle
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, O.P., but it's still not firing. The problem must be with a conflict in the two functions themselves. They're usually not called one after the other unless the first has completely finished already. That's why i thought using setTimeout would work here, but something about the fist function firing just doesn't want the second to work.

  • #6
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Have you tried having the first function call the second function via a setTimeout statement?

  • #7
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    just got another thought.. try this out...
    Code:
    <a href="javascript:;" onclick="parent.func1();setTimeout('parent.func2()', 1000) return false;">Click here</a>
    Thanks & Regards,
    Niral Soni

  • #8
    New Coder
    Join Date
    Aug 2010
    Location
    Seattle
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For the life of me, nothing works (and ironboy, I need the two functions to be able to fire independently - though, just to try it, I had the second func fire on a condition inside the first and even that didn't work!). There just must be some irreconcilable conflict between the two.

    I've giving up in this for now. It would have been nice to get it to work, but it's not that important, and not that necessary a use of my time.

    Thanks for the replies, all. Appreciated as always.
    ~gyz

  • #9
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    can you post func1 and func2 here aswell? so we can see them?

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    I think you just have some cockpit error.

    A simple test works fine.

    **** FILE: junk1.html *****
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function func1( )
    {
        document.body.style.backgroundColor = "yellow";
    }
    function func2( )
    {
        document.body.style.color = "green";
        document.body.style.fontSize = "xx-large";
    }
    </script>
    </head>
    <body>
    <h2>Isn't this fun?<h2>
    <h3>Just some random text</h3>
    <form>
    <input type="button" value="Test func1" onclick="func1();"/>
    <input type="button" value="Test func2" onclick="func2();"/>
    </form>
    <br/><br/>
    <iframe src="junk2.html" style="width: 100%; height: 200px;"></iframe>
    </body>
    </html>
    **** FILE: junk2.html *****
    Code:
    <html>
    <body>
    <a href="#" onclick="parent.func1(); setTimeout(parent.func2,3000); return false;">
       CLICK HERE TO TRY IT
    </a>
    </body>
    </html>
    The buttons in junk1.html are just to show what the two functions do.

    If you try pushing them, then hit refresh before clicking on the link in the iframe.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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