/    Sign up×
Community /Pin to ProfileBookmark

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!! [url]http://javascript.internet.com[/url]
Created by: Anonymous | [url]http://musikimiz.googlepages.comLicensed[/url] 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
// [url]http://simonwillison.net/2004/May/26/addLoadEvent/[/url]
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();
});

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJul 31.2010 — 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.
Copy linkTweet thisAlerts:
@moonfighterauthorJul 31.2010 — 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
Copy linkTweet thisAlerts:
@moonfighterauthorJul 31.2010 — 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>
Copy linkTweet thisAlerts:
@JMRKERJul 31.2010 — Still need more information:

  • 1. How many time periods?


  • 2. What are the image links for a particular time period?
  • Copy linkTweet thisAlerts:
    @moonfighterauthorJul 31.2010 — 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
    Copy linkTweet thisAlerts:
    @JMRKERAug 01.2010 — See if you can modify this to fit your needs.
    <i>
    </i>&lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Song Times&lt;/title&gt;
    &lt;script type="text/javascript"&gt;

    /* 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 &lt; 10) { h = '0'+h; }
    var m = t.getMinutes(); if (m &lt; 10) { m = '0'+m; }
    var findTime = h+':'+m;
    var ndx = -1;
    var i=0;
    while ((ndx &lt; 0) &amp;&amp; (i &lt; TimeRanges.length-1)) {
    i++; if (findTime &lt; TimeRanges[i]) { ndx = i-1; break; }
    }
    var el=document.getElementById('myimage');
    el.src = baseURL+songArray[ndx];;
    el.alt = songArray[ndx];

    // for testing purposes only <br/>
    // 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(); });

    &lt;/script&gt;

    &lt;/head&gt;
    &lt;body&gt;
    &lt;img id="myimage" src="" alt=""&gt;
    &lt;/body&gt;
    &lt;/html&gt;


    I don't have your song links to test it out

    but it works fine with the example images.
    Copy linkTweet thisAlerts:
    @moonfighterauthorAug 01.2010 — it works fine with the img, i will try it with the songs.

    thank you you are a good one ?
    Copy linkTweet thisAlerts:
    @moonfighterauthorAug 01.2010 — 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
    Copy linkTweet thisAlerts:
    @JMRKERAug 01.2010 — 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[/QUOTE]


    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.
    Copy linkTweet thisAlerts:
    @moonfighterauthorAug 01.2010 — 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.
    <i>
    </i>&lt;script language="JavaScript" type="text/javascript"&gt;
    &lt;!-- Copyright 2007, Sandeep Gangadharan --&gt;
    &lt;!-- For more free scripts go to <a href="http://sivamdesign.com/scripts/">http://sivamdesign.com/scripts/</a> --&gt;
    &lt;!--
    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 &gt;= 7 &amp;&amp; hour &lt; 12) { document.write("&lt;embed src='" + morning + "' hidden='true' border='0' autostart='true' loop='true'&gt;") }
    if (hour == 12) { document.write("&lt;embed src='" + noon + "' hidden='true' border='0' autostart='true' loop='true'&gt;") }
    if (hour &gt; 12 &amp;&amp; hour &lt; 17) { document.write("&lt;embed src='" + afternoon + "' hidden='true' border='0' autostart='true' loop='true'&gt;") }
    if (hour &gt;= 17 &amp;&amp; hour &lt; 22) { document.write("&lt;embed src='" + evening + "' hidden='true' border='0' autostart='true' loop='true'&gt;") }
    else { document.write("&lt;embed src='" + def + "' hidden='true' border='0' autostart='true' loop='true'&gt;") }
    }
    sivamtime()
    // --&gt;
    &lt;/script&gt;

    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
    Copy linkTweet thisAlerts:
    @Ay__351_eAug 04.2010 — 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

    <br/>
    &lt;script type="text/javascript"&gt;
    // 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 &gt;= times[0] &amp;&amp; t&lt; times[1] ) { img.src=image[0]; el.src=songs[0]; }
    if (t &gt;= times[1] &amp;&amp; t&lt; times[2] ) { img.src=image[1]; el.src=songs[1]; }
    if (t &gt;= times[2] &amp;&amp; t&lt; times[3] ) { img.src=image[2]; el.src=songs[2]; }
    if (t &gt;= times[3] &amp;&amp; t&lt; times[4] ) { img.src=image[3]; el.src=songs[3]; }
    if (t &gt;= times[4] || t&lt; 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(); });

    &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;embed id="music" src="" hidden="true" border="0" autostart="true" loop="true"&gt;&lt;/embed&gt;
    &lt;img id="myimages" src="" alt="my images" width="200" height="300"&gt;

    How to open a dynamic page in IFRAME using JavaScript?

    http://www.devarticles.com/c/a/JavaScript/Working-with-IFRAME-in-JavaScript/3/

    http://www.devarticles.com/c/a/JavaScript/Working-with-IFRAME-in-JavaScript/3/

    http://www.devarticles.com/c/a/JavaScript/Working-with-IFRAME-in-JavaScript/5/
    Copy linkTweet thisAlerts:
    @Ay__351_eAug 04.2010 — Jmrker,

    I think the while loop in your code.

    I must use loop to write a short code.
    Copy linkTweet thisAlerts:
    @JMRKERAug 05.2010 — Jmrker,

    I think the while loop in your code.

    I must use loop to write a short code.[/QUOTE]


    I don't understand the question.

    ?
    Copy linkTweet thisAlerts:
    @moonfighterauthorAug 09.2010 — Thank u guys but i have to mention that Ay&#351;e's script workfine with FF but doesn't work on IE. is there any good idea how to fix it?
    ×

    Success!

    Help @moonfighter spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.11,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...