SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I fix the page/content delay? Novice

    Hi all still very new to javascript but have a slight problem with my counter:

    Everything works ok but when my page loads or even when I refresh the page the javascript content has a short delay then appears???? why is that?

    I haven't shown the .js file I thought it might just be something out of position or have you had this problem before? If you want to see the .js please say? thanks

    html

    ...
    HTML Code:
    </head>
    <body>
    <script type="text/javascript" src="countdown.js"></script>
    ....
    
    <p>
      Event1 <span class="daysTill" id="e1"> </span> days<br>
      Event2 <span class="daysTill" id="e2"> </span> days<br>
      Event3 <span class="daysTill" id="e3"> </span> day<br>
      </p>
    
    
    ..</html>
    thanks CB

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are calling a timer or running a long process in the script, the page load will pause until the process returns.Put the script at the end of the page.

  3. #3
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers mrhoo

    I tried the below but it still has a blink? Everything else on the page is loaded but just that which has a slight delay.. sort of a blink/flash delay?
    HTML Code:
    ..
    <script type="text/javascript" src="countdown.js"></script>
    </body>
    Is it anything to do with:

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    .js if it helps:

    Code JavaScript:
    // JavaScript Document
    window.onload = showDays;
     
    function showDays() {
    		var allTags = document.getElementsByTagName("*");
     
    		for (var i=0;i<allTags.length; i++) {
    			if (allTags[i].className.indexOf("daysTill") > -1) {
    				allTags[i].innerHTML = showTheDaysTill(allTags[i].id);
    			}
    		}
     
    		function showTheDaysTill(thisDate) {
    			var theDays;
     
    			switch(thisDate) {
    				case "e1":
    					theDays = daysTill(8,23)
    					break;
    				case "e2":
    					theDays = daysTill(8,24)
    					break;
    				case "nye":
    					theDays = daysTill(12,31)
    					break;
    				default:
    			}
    			return theDays + " ";
    		}
     
    		function daysTill(mm,dd) {
    			var now = new Date();
    			var inDate = new Date(now.getFullYear(),mm-1,dd);
     
    			if (inDate.getTime() < now.getTime()) {
    				inDate.setYear(now.getFullYear()+1);
    			}
     
    			return (Math.ceil(dayToDays(inDate) - dayToDays(now)));
    		}
     
    		function dayToDays(inTime) {
    			return (inTime.getTime() / (1000 * 60 * 60 * 24));
    		}
    }

    Thanks
    Last edited by computerbarry; Apr 16, 2008 at 16:13.

  4. #4
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any ideas anybody? Not really a big issue but I would like it to work with out the delay.

    On another note.

    I've just realized that with the code above if I change my date & time setting on my computer the days of the events change also... so does that mean if somebody has there computer set in 2005 it will reflect on the counter?

    If so? How do I work around this problem? So everybody sees the counter at the proper date?

    Thanks CB

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your script is rewriting the page content after it has loaded, and so redraws the screen.

    The best way to have the content you want appear on the page when it loads is to write the script on the server- you can get the server time that way as well.

    You could use javascript document.write instead of dom methods to write the content as the page loads.
    If you do the javascript, change the doctype from strict to transitional, or none, and hope the user's clock is set correctly.

  6. #6
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks mrhoo

    The best way to have the content you want appear on the page when it loads is to write the script on the server- you can get the server time that way as well.
    So what will I need to do? Php? or is there a way with js?

    and hope the user's clock is set correctly.
    thats a risk I cant take..?

    Cheers


Bookmarks

Posting Permissions

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