www.webdeveloper.com
Results 1 to 15 of 15

Thread: edit functions time to be hours:minuts:seconds

  1. #1
    Join Date
    Jul 2010
    Posts
    9

    Question edit functions time to be hours:minuts:seconds

    Hi, i found this script around. and need some help tomake it works as i wish.
    to add minuts and seconds so can change the background ex. 07:45:00 instead of just hours only.

    thanx for help

    /* This script and many more are available free online at
    The JavaScript Source!! http://javascript.internet.com
    Created by: Anonymous | http://musikimiz.googlepages.comLicensed under: Creative Commons License
    */

    function pixTimeChange() {
    var t=new Date();
    var h = t.getHours();
    var r1="pic1.gif";
    var r2="pic2.gif";
    var el=document.getElementById('myimage');

    // See the time below. Note: The time is in 24 hour format.
    // In the example here, "7" = 7 AM; "17" =5PM.
    el.src = (h>=7 && h<17) ? r1 : r2;
    }

    // Multiple onload function created by: Simon Willison
    // http://simonwillison.net/2004/May/26/addLoadEvent/
    function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
    window.onload = func;
    } else {
    window.onload = function() {
    if (oldonload) {
    oldonload();
    }
    func();
    }
    }
    }

    addLoadEvent(function() {
    pixTimeChange();
    });

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355

    Question

    The code you posted does not display the time in any form.
    It only selects the picture to display between a particular hour of the day.
    You could expand on this to display different pictures for every hour of the day
    but there is no logic to determine what is to be displayed with min:sec values.

    If you want the actual time display to change with min:sec,
    you need to provide some more code to where the time is set and displayed,
    not just a function to determine what images are displayed.

    BTW: You should enclose you code between [ code] and [ /code] tags (without the spaces)
    to make it stand out better in your posting.

  3. #3
    Join Date
    Jul 2010
    Posts
    9
    thanx for response JMRKER,
    this is true the script pictures but in hours i need to add minuts and second this is what i want to display pictures ex. 7.30 or maybe 8.45 this is my point.
    thanx anyway.
    can u plz rescript it for me itryed but no results.
    regards

  4. #4
    Join Date
    Jul 2010
    Posts
    9

    plz help

    this is another script with the same problem. no min or sec found.
    <script language="JavaScript">

    day=new Date() //..get the date

    x=day.getHours() //..get the hour

    if(x>=0 && x<4) {

    document.write('<style type="text/css">body{background: white url(1st.jpg); color: black}"></style>')

    } else

    if(x>=4 && x<12) {

    document.write('<style type="text/css">body{background: white url(2nd.jpg); color: black}</style>')

    } else

    if(x>=12 && x<18) {

    document.write('<style type="text/css">body{background: white url(3rd.jpg); color: black}</style>')

    } else

    if (x>=18 && x<24) {

    document.write('<style type="text/css">body{background: white url(4th.jpg); color: black}</style>')

    }

    </script>

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355
    Still need more information:

    1. How many time periods?

    2. What are the image links for a particular time period?

  6. #6
    Join Date
    Jul 2010
    Posts
    9
    it is actully mp3 files instead pictures but almost same script and it has to be 5 times
    morning 5.30
    noon 12.15
    afternoon 15.25
    evening 19.05
    night 22.10

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355

    Lightbulb Consider these changes ...

    See if you can modify this to fit your needs.
    Code:
    <html>
    <head>
    <title>Song Times</title>
    <script type="text/javascript">
    
    /* This script and many more are available free online at
    The JavaScript Source!! http://javascript.internet.com
    Created by: Anonymous | http://musikimiz.googlepages.comLicensed under: Creative Commons License
    Modified by: jmrker } http://www.webdeveloper.com/forum/showthread.php?p=1105663#post1105663
    */
    
    var TimeRanges = ['00:00','05:30','12:15','19:05','22:10','24:00'];
    // var songArray = ['song0.mp3','song1.mp3','song2.mp3','song3.mp3','song4.mp3',''];
    
    // following for testing purposes
    var baseURL = 'http://www.nova.edu/hpd/otm/pics/4fun/';
    var songArray = ['11.jpg','12.jpg','13.jpg','14.jpg','15.jpg',''];
    
    function pixTimeChange() {
      var t=new Date();
      var h = t.getHours();    if (h < 10) { h = '0'+h; }
      var m = t.getMinutes();  if (m < 10) { m = '0'+m; } 
      var findTime = h+':'+m;
      var ndx = -1;
      var i=0;
      while ((ndx < 0) && (i < TimeRanges.length-1)) {
        i++;  if (findTime < TimeRanges[i]) { ndx = i-1; break; }
      }
      var el=document.getElementById('myimage');
      el.src = baseURL+songArray[ndx];;
      el.alt = songArray[ndx];
      
    // for testing purposes only  
    //  alert(findTime + ' | ' + ndx + ' | ' + TimeRanges[ndx] + ' | ' + el.src);  
    
    }
    
    // Multiple onload function created by: Simon Willison
    // http://simonwillison.net/2004/May/26/addLoadEvent/
    function addLoadEvent(func) {
      var oldonload = window.onload;
      if (typeof window.onload != 'function') {
        window.onload = func;
      } else {
        window.onload = function() {
          if (oldonload) { oldonload(); }
          func();
        }
      }
    }
    
    addLoadEvent(function() { pixTimeChange(); }); 
    
    </script>
    
    </head>
    <body>
    <img id="myimage" src="" alt="">
    </body>
    </html>
    I don't have your song links to test it out
    but it works fine with the example images.

  8. #8
    Join Date
    Jul 2010
    Posts
    9
    it works fine with the img, i will try it with the songs.
    thank you you are a good one

  9. #9
    Join Date
    Jul 2010
    Posts
    9
    is it possible to play around this script to make it redirect to another page in an iframe?
    thank you again!!
    if yes plz help me

  10. #10
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355

    Exclamation

    Quote Originally Posted by moonfighter View Post
    is it possible to play around this script to make it redirect to another page in an iframe?
    thank you again!!
    if yes plz help me
    Probably yes.
    You will need to define what it is that you are trying to do for me.
    I'm tired of guessing what you want to do and then you changing the requirements.
    Define what you want before you begin the design/implementation phase.
    Show some code.

  11. #11
    Join Date
    Jul 2010
    Posts
    9
    i want to make 365 days in each day changings 5 times images and 5 to 6 mp3 bg music in 2 diffrent iframes
    the music must be diffrent URL's cause it's radio stations and look at this ex.
    Code:
    <script language="JavaScript" type="text/javascript">
    <!-- Copyright 2007, Sandeep Gangadharan -->
    <!-- For more free scripts go to http://sivamdesign.com/scripts/ -->
    <!--
    function sivamtime() {
    	now=new Date();
    	hour=now.getHours();
     var morning = "morning.mp3";     // change name of audio files here to suit your needs
     var noon = "noon.mp3";           // you can have any type of audio files
     var afternoon = "afternoon.mp3";
     var evening = "evening.mp3";
     var def = "default.mp3";
    
      if (hour >= 7 && hour < 12) { document.write("<embed src='" + morning + "' hidden='true' border='0' autostart='true' loop='true'>") }
      if (hour == 12) { document.write("<embed src='" + noon + "' hidden='true' border='0' autostart='true' loop='true'>") }
      if (hour > 12 && hour < 17) { document.write("<embed src='" + afternoon + "' hidden='true' border='0' autostart='true' loop='true'>") }
      if (hour >= 17 && hour < 22) { document.write("<embed src='" + evening + "' hidden='true' border='0' autostart='true' loop='true'>") }
      else { document.write("<embed src='" + def + "' hidden='true' border='0' autostart='true' loop='true'>") }
    }
    sivamtime()
    // -->
    </script>
    as you see missing minuts

    and i'm asking to lern more about JS and you are an expert no one could help me as you did.
    by the way i found something you may take a look it may helps
    http://www.javascriptkit.com/script/script2/alarm.shtml
    Last edited by Kor; 08-02-2010 at 03:47 AM. Reason: wrap the code [code][/code]

  12. #12
    Join Date
    Sep 2007
    Posts
    315
    I wrote a code. I used getTime() and setHours();

    http://www.w3schools.com/js/js_obj_date.asp

    http://www.w3schools.com/jsref/jsref_obj_date.asp

    Code:
      
    <script type="text/javascript">
    // http://www.webdeveloper.com/forum/showthread.php?t=233628
    /*
    The Time Through Ages. In the Name of Allah, Most Gracious, Most Merciful.  1. By the Time,  2. Verily Man is in loss, 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.
    */
    function swapMusicAndImage() {
    
    var d = new Date();
    // alert(d)
    var t = d.getTime();
    
    // alert(t);
    
    var times = [d.setHours(5, 30), d.setHours(12,15), d.setHours(15,15), d.setHours(19, 5), d.setHours(22, 10) ];
    
    // alert(times[0]);
    
    var songs = ["morning.mp3","noon.mp3", "afternoon.mp3", "evening.mp3", "night.mp3" ];
    
    var image = ["morning.jpg","noon.jpg", "afternoon.jpg", "evening.jpg", "night.jpg" ];
    
    var img = document.getElementById("myimages");
    
    var el = document.getElementById("music");
    
    if( t >= times[0] && t< times[1] )  { img.src=image[0]; el.src=songs[0];  }
    if (t >= times[1] && t< times[2] )  { img.src=image[1]; el.src=songs[1];  }
    if (t >= times[2] && t< times[3] )  { img.src=image[2]; el.src=songs[2];  }
    if (t >= times[3] && t< times[4] )  { img.src=image[3]; el.src=songs[3];  }
    if (t >= times[4] || t< times[0] )  { img.src=image[4]; el.src=songs[4];  }
    
    alert(document.body.innerHTML);
    
    }
    
    
    // Multiple onload function written by: Simon Willison
    // http://simonwillison.net/2004/May/26/addLoadEvent/
    function addLoadEvent(func) {
      var oldonload = window.onload;
      if (typeof window.onload != 'function') {
        window.onload = func;
      } else {
        window.onload = function() {
          if (oldonload) { oldonload(); }
          func();
        }
      }
    }
    
    addLoadEvent(function() { swapMusicAndImage(); });
    
    </script>
    </head>
    <body>
    <embed id="music" src="" hidden="true" border="0" autostart="true" loop="true"></embed>
    <img id="myimages" src="" alt="my images" width="200" height="300">
    How to open a dynamic page in IFRAME using JavaScript?

    http://www.devarticles.com/c/a/JavaS...-JavaScript/3/

    http://www.devarticles.com/c/a/JavaS...-JavaScript/3/

    http://www.devarticles.com/c/a/JavaS...-JavaScript/5/
    Last edited by Ayşe; 08-04-2010 at 06:32 AM.
    Bismillahirrahmanirrahîm
    Hamd, Âlemlerin Rabbi, Rahmân, Rahîm, hesap ve ceza gününün (ahiret gününün) maliki Allah'a mahsustur. (Allahım!) Yalnız sana ibadet ederiz ve yalnız senden yardım dileriz. Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

  13. #13
    Join Date
    Sep 2007
    Posts
    315
    Jmrker,

    I think the while loop in your code.

    I must use loop to write a short code.
    Bismillahirrahmanirrahîm
    Hamd, Âlemlerin Rabbi, Rahmân, Rahîm, hesap ve ceza gününün (ahiret gününün) maliki Allah'a mahsustur. (Allahım!) Yalnız sana ibadet ederiz ve yalnız senden yardım dileriz. Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

  14. #14
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355

    Question Huh...

    Quote Originally Posted by Ayşe View Post
    Jmrker,

    I think the while loop in your code.

    I must use loop to write a short code.
    I don't understand the question.

  15. #15
    Join Date
    Jul 2010
    Posts
    9
    Thank u guys but i have to mention that Ayşe's script workfine with FF but doesn't work on IE. is there any good idea how to fix it?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles