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

    Call variable in (document).ready, access in (window).load

    I'm trying to get my variables to work across document.ready and window.load functions, but they don't seem to be working. Here's my code being called in document.ready:

    Code:
    		function playHeaderIntro(){
    	    	setTimeout(function() {
    		  		homeInterval = setInterval(homeRightArrowClick, 5000);
    		  		processing = false;
    	  		}, 3300);
    		};
    The two variables I'm defining are at the bottom - "homeInterval" and "processing." I have various functions that need to interact with these two variables in my window.load section of my JS, but they don't seem to be working. For example, here's a function in window.load that calls them:

    Code:
    $('#home-rightarrow').click(function () {
      			clearInterval(homeInterval);
      			if (processing == false) {
    	  			homeRightArrowClick();
      			};
      		});
    They were called and changed just fine when I defined them in window.load, but unfortunately I can't do that and they have to be called in document.ready. I'm assuming this is a global variable issue, but I tried what people suggested and put "window." in front of both variable names throughout the JS and it didn't seem to work, so I probably didn't do it right. I'd really appreciate any help... thank you!!

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by yanathin View Post
    I'm assuming this is a global variable issue, but I tried what people suggested and put "window." in front of both variable names throughout the JS and it didn't seem to work, so I probably didn't do it right. I'd really appreciate any help... thank you!!
    No, this is a timing issue. Window load fires before
    document ready, so the variables are not yet
    ready for use at window load.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    Quote Originally Posted by DaveyErwin View Post
    No, this is a timing issue. Window load fires before
    document ready, so the variables are not yet
    ready for use at window load.
    Wrong. document.ready fires first than window.onload. If there are multiple images on the page, all the images have to be fully downloaded first before window.onload is fired. Whereas, document.ready will not wait for all the resources to be loaded, it will fire once the DOM is ready.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    Declare the variables outside the document.ready and any anonymous/named functions.

    Code:
    <script type="text/javascript">
    var homeInterval, processing;
    
    //...your other code here
    </script>

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Quote Originally Posted by glenngv View Post
    Wrong. document.ready fires first than window.onload.
    Yes but in this case there is a 3.3 second delay between document.ready and the variables being defined so window.onload might trigger first.

    It is 99.99% likely that simply correctly placing the script immediately before the </body> tag will allow both the document.ready and window.onload references to be removed and for the entire script to run from within a single anonymous function the way that 99.99%+ of properly written JavaScripts can run.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    Right. I didn't notice the setTimeout.


  •  

    Posting Permissions

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