View Full Version : javascript problem
ThE_cHaD24
01-09-2009, 10:33 AM
Hi everyone...I would like to know if anyone could help me with my piece of code...
Basically im trying to create a timer, what i need to do is have a certain function activate at a certain time exactly, eg. at 18:39:32 myfunc() must be activated....
Is there a way to do this? Thanks :)
pghcollectibles
01-09-2009, 11:08 AM
this would have to be an open script only operating with the users browser to actually execute. javascript is executed by the user's browser it doesnt have any effect on the server until you get into ajax and stuff like that. plus if the user closes the page before the time hits or doesnt open the page until after that time it wont execute.
either you dont care or have something else in mind, which in that case here (http://www.w3schools.com/jsref/jsref_obj_date.asp)'s a link that gets into the detail of the javascript date objects. php also has various timing functions as well. the main thing is, what are you expecting the code to do.
or maybe you need to look into what is involved in setting up a cron job, which can allow your server to execute a script at a time or interval. it wont affect a user that doesnt happen to be connecting with your server at that time though.
maybe you could tell us what you are trying to do.
ThE_cHaD24
01-09-2009, 12:13 PM
well what im trying to do is to have a sound play at a certain time (specified by the user)...It is basically a count down until that time, and it is going to be client based, so im not looking for a php function, just javascript...What is this cron job about? Because i think that is the main function of my code...:)
pghcollectibles
01-09-2009, 02:54 PM
ok for this to work the user will need to have the page open at the time you want the function to start. thats all.
from this (http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_clock) page (which i found from this (http://www.w3schools.com/js/js_examples.asp) page with the link, "a clock created with a timing event") i copied the code below:
<html>
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout('startTime()',500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script>
</head>
<body onload="startTime()">
<div id="txt"></div>
</body>
</html>
for your purpose, i would change it to this:
<html>
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
var ah="18";
var am="39";
var as="32";
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
if (h==ah && m==am && s==as){
document.getElementById('sound').innerHTML="<embed src='sound.wav' />";
//or
document.getElementById('sound').innerHTML="<img dynsrc='sound.wav' />";
//or
document.getElementById('sound').innerHTML="<object classid='clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95'><param name='FileName' value='sound.wav' /></object>";
}
document.getElementById('txt').innerHTML="the alarm is set to: "+ah+":"+am+":"+as+"<br />The current time is: "+h+":"+m+":"+s;
t=setTimeout('startTime()',500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script>
</head>
<body onload="startTime()">
<div id="txt"></div>
<div id="sound"></div>
</body>
</html>
i tried to learn a little from here (http://www.w3schools.com/media/default.asp), but it wasnt as informative as i would have liked.
i can tell you, i have linux umbuntu 8.0 with firefox 3.0 and i didnt hear any of those sounds on my machine, however i have heard sounds on my windows machine before from those types of objects. maybe this box isnt set up enough for that yet. all i can say is test it for yourself to see how it works for you. you will need to reference whatever sound file you are using.
ThE_cHaD24
01-09-2009, 03:54 PM
Thanks i tried it and it worked! thank you so much for your help...BTW firefox has a problem playing wav sound files, i dont know why but it does...So i use internet explorer to play wav files, but under normal circumstances i would normally use firefox:)
pghcollectibles
01-09-2009, 04:26 PM
wav files were designed by ibm and microsoft perhaps there's something about that. "most modern browsers support wavs" lol
ThE_cHaD24
01-09-2009, 06:01 PM
lol, well i googled it, and it came up with various different forums discussing why firefox doesn't play them, you should check it out:)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.