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 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question function in the onLoad

    There is a Timeout function that I'm calling in the OnLoad in the Body tag.
    I want the function to start if a certain variable is false. This variable is defined as 'false' by default but later if a button is clicked on it turns as 'true'.
    Therefore my function will always start because at the beguinning the variable is always false. What can I do to make the function check the current state of the variable? Or is there another way to do what I want to?
    If my explanation is not clear, please let me know.

  • #2
    Regular Coder
    Join Date
    Nov 2004
    Location
    The land of chocolate
    Posts
    226
    Thanks
    1
    Thanked 16 Times in 16 Posts
    You can set your variable to true when you declare it.
    Code:
    var variablename=true;
    Make sure you put that line outside of your function, and the variable will have a value of "true" as the function is initiated with onLoad. Of course, I don't see the context of your code (for instance, that button), so it might be possible this will create conflicts with the rest of the code...

    EDIT: Hmm, the more I think about it, the more it seems as if this manner of coding isn't going to work at all. A function which is initiated at onLoad should either always work, either never. You can hardly check for a boolean value at onLoad: it's either true or false, but it won't change anymore. In other words, if the function is set to start when the variable is false, it will indeed start when the value at onLoad is false, but when the value at onLoad is true, it will never start. If you change the value somewhere in between (that is what your button does, I think), the function will still not start because onLoad has already happened. I hope I make myself clear with these ramblings, otherwise I'll try to find another way of explaining it...

    In any case, why don't you just start the function when the button is pressed? If that is not what you want to do, then you might want to post your code and explain your intentions...
    Last edited by snowieken; 04-12-2005 at 02:17 PM.
    -Snow

    Susie, if you want to see your doll again, leave $100 in this envelope by the tree out front. Do NOT call the police. You CANNOT trace us. You CANNOT find us.

    Sincerely,
    - Calvin.

  • #3
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So the exact situation is:

    There is a slideshow that must start after a mp3 file has finnished, and that is after a certain timeout after the Load, that's why I have to put the function that starts the slide in the onLoad part.
    There is a 'mute' button for the mp3 file, and I want if somebody clicks on it, the clicking to cancel the future slide too. If nobody clicks the 'mute' button the slide should start after the mp3.



    If you happen to know a way to do this, please let me know
    Thanks.

  • #4
    Regular Coder
    Join Date
    Jul 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Though I'm not clear on your question, it sounds something like this (an example): a timer is set to, say, 30 seconds and it loads 'onload'. When the time is up, a window pops up. The objective is that when the user would press a certain button, the timer should stop and the window should never pop up. In other words, you'd like to cancel the onload event, as soon as a certain action is done...

    Providing that this is the case, all you have to do is set the variable *within* the timer function, and not as a condition for triggering the function. This way, when the time is up, it will consult this variable to see if its true or false, and then act accordingly. If the variable has been set to true (or false, whichever way you set it), it will trigger the function, otherwise it will not.

    Something like this:

    PHP Code:
    <html>
    <
    head>
    <
    script language="JavaScript">
    var 
    nopop=false
    function condition()
    {
    if (
    nopop == false) {
    window.open('http://mywebsite.com');
    }
    }
    function 
    on_load()
    {
    setTimeout ("condition()",3000);
    }
    </script>
    </head>
    <BODY onload="on_load();">
    <input type="button" value="No popup please!" onClick="nopop=true;">
    </body>
    </html> 
    EDIT: after reading your clarification reply, it would seem that the same method should work for you. Just substitute the window popup with the slideshow, and the button with the mute button.
    Last edited by Larry770; 04-12-2005 at 03:07 PM.

  • #5
    Regular Coder
    Join Date
    Nov 2004
    Location
    The land of chocolate
    Posts
    226
    Thanks
    1
    Thanked 16 Times in 16 Posts
    I haven't got time to test this properly, but try the following:

    When you set the Timeout(), give it an ID like this:
    Code:
    blah = window.setTimeout("your code","your time in milliseconds")
    Then, add an onclick function to your button which does the following:
    Code:
      clearTimeout("blah");
    where "blah" is of course the ID you gave to the setTimeout().

    As I said, I don't have time to test it, but it should work.

    EDIT: Larry's code above actually works better, I hadn't even thought of that.
    Last edited by snowieken; 04-12-2005 at 02:53 PM.
    -Snow

    Susie, if you want to see your doll again, leave $100 in this envelope by the tree out front. Do NOT call the police. You CANNOT trace us. You CANNOT find us.

    Sincerely,
    - Calvin.

  • #6
    Regular Coder
    Join Date
    Jul 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just tested my script and it doesnt work. But the general principle should be fine...

    EDIT- just fixed it, it should be fine now.

    Best of luck,
    Larry
    Last edited by Larry770; 04-12-2005 at 03:03 PM.

  • #7
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My code is exactly as yours. And this is not the way it could work.
    If you see your code the checking of 'if(somevariable)' is happening at the start( on the load of the page) when the variable would be always false.
    It doesn't matter if the variable changes after this because the function would be already started counting.



    Quote Originally Posted by Larry770
    Though I'm not clear on your question, it sounds something like this (an example): a timer is set to, say, 30 seconds and it loads 'onload'. When the time is up, a window pops up. The objective is that when the user would press a certain button, the timer should stop and the window should never pop up. In other words, you'd like to cancel the onload event, as soon as a certain action is done...

    Providing that this is the case, all you have to do is set the variable *within* the timer function, and not as a condition for triggering the function. This way, when the time is up, it will consult this variable to see if its true or false, and then act accordingly. If the variable has been set to true (or false, whichever way you set it), it will trigger the function, otherwise it will not.

    Something like this:

    PHP Code:
    <html>
    <
    head>
    <
    script language="JavaScript">
    var 
    nopop=false
    function condition()
    {
    if (
    nopop == false) {
    window.open('http://mywebsite.com');
    }
    }
    function 
    on_load()
    {
    setTimeout ("condition()",3000);
    }
    </script>
    </head>
    <BODY onload="on_load();">
    <input type="button" value="No popup please!" onClick="nopop=true;">
    </body>
    </html> 
    EDIT: after reading your clarification reply, it would seem that the same method should work for you. Just substitute the window popup with the slideshow, and the button with the mute button.

  • #8
    Regular Coder
    Join Date
    Nov 2004
    Location
    The land of chocolate
    Posts
    226
    Thanks
    1
    Thanked 16 Times in 16 Posts
    Hmm, I'm not exactly sure I understand the problem correctly. Yes, the function would still be counting, but as it reaches the end of the Timeout(), it checks the variable. If the button was not pressed, the variable is still set to false and the slideshow will start. But if the button is pressed while the timer was counting, the variable will be set to true in the mean time... and if that variable is set to true, it doesn't do the slideshow.

    It should work. But if it doesn't, could you show us your code perhaps?
    -Snow

    Susie, if you want to see your doll again, leave $100 in this envelope by the tree out front. Do NOT call the police. You CANNOT trace us. You CANNOT find us.

    Sincerely,
    - Calvin.

  • #9
    Regular Coder
    Join Date
    Jul 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    As Snow just stated, the timer is still counting, regardless of what state the variable is in. The key is that the counter does nothing on its own, but rather triggers a function that consults another variable in order to carry out the action. Its a somewhat non-straightforward method, but it should work no problem. Alternitavely you could do as Snow posted earlier on- 'clearTimeout', and this should effectively cut the timer itself. The advantage in the first method would be that you have many options to further manipulate the action with additional functions and variables, should you need this.

  • #10
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It is difficult to show all the code because it is separated in a lot of different files, but is like this:

    var btn_clck=false;

    function slideshow(b){
    if(b==1){
    window.setTimeout("MM_swapImage(\'slide1\',\'\',\'media/3a.jpg\',1);", 4500);
    window.setTimeout("MM_swapImage(\'slide1\',\'\',\'media/3b.jpg\',1);", 9000);
    }
    if(b==2){
    window.setTimeout("MM_swapImage(\'slide2\',\'\',\'media/4a.gif\',1);", 37000);
    window.setTimeout("MM_swapImage(\'slide2\',\'\',\'media/4b.gif\',1);", 43000);
    window.setTimeout("MM_swapImage(\'slide2\',\'\',\'media/4c.gif\',1);", 49000);
    window.setTimeout("MM_swapImage(\'slide2\',\'\',\'media/4d.gif\',1);", 55000);
    window.setTimeout("MM_swapImage(\'slide2\',\'\',\'media/blank.gif\',1);", 61000);
    window.setTimeout("MM_swapImage(\'base\',\'\',\'media/blank.gif\',1);", 61000);
    window.setTimeout("MM_swapImage(\'basesm\',\'\',\'media/blank.gif\',1);", 61000);
    }
    }

    ..........................

    <body onLoad="slideshow(2)">
    </body>

    .............................

    so I have to make a separate function of any sepparate element of the slide .... Is there a more simple way to do it?
    Last edited by gebezis; 04-12-2005 at 03:36 PM.

  • #11
    Regular Coder
    Join Date
    Jul 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Its not really too complicated- instead of calling the slideshow directly, have it called through a function. Eg. 'onload' would call a function that has a timeOut. When the timeOut is up, it calls a function called 'check_btn', which in turn would say 'if btn_click=false then run the slideshow'. Then your button, if clicked, would simply set 'btn_click' to true, which would cause the slideshow not to show.

    However I see from your example that the timeOut function is not the cause of the slideshow, but the slideshow itself. In this case (supposing that you have many such slideshows and different variables), you might want to in fact go with using the clearTimeout (though you'd still have to tell it which one to clear, which seems to be what you didnt want...)

    I think that it might help if you paste the full script here, this might allow for either myself or someone else to better understand what your need/thought is. (Because as of now the code that you pasted has nothing at all to do with the 'btn_clk' variable, which makes it pretty dificult to understand your intention...)


  •  

    Posting Permissions

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