3

I'm an amateur web designer tasked to create a small intranet site for our department. I need help in setting up an embedded sound file (wav or mp3) to play at specific times of the day using Javascript. This will serve as an alarm for us to perform tasks which are time-critical. Example would be to play the sound file a certain number of minutes before the intended time eg. 9:29AM, 5:58PM. Any help will be greatly appreciated. Thanks in advance.

  • Whilst this is a valid question, it's a horrible idea in my opinion! Playing sounds in JavaScript as a schedule/reminder system is the wrong tools for the wrong job! – Widor Nov 02 '11 at 12:35
  • 3
    And you will stay amateur if you don't start doing the things by yourself. What have you find so far on the topic? What have you done so far? What seems to be the problem? – Bakudan Nov 02 '11 at 12:37
  • I am not a hardcore javascript programmer and I've tried searching for scripts online but to no avail. I understand that this could be annoying on the part of the user. I will reconsider whether to implement such script for our site, but for personal reasons I would still appreciate if you can teach me how to implement this in Javascript. Thank you again. – Jon Milford Nov 02 '11 at 13:26
  • @Widor, is it the wrong tools? JS is a type of programming, why not use it as a solution? with HTML5 and webgl round the corner, you'd be hard pressed to find a better tool – Abe Petrillo Nov 03 '11 at 13:13
  • @AbePetrillo I don't think it's necessarily the wrong tool to play sounds on a webpage _per se_, rather it's the wrong tool for the application the OP describes - if you want a reliable schedule reminder, don't run it in a browser tab that could be thwarted by a misplaced `ctrl+w` or rendered useless by noscript! – Widor Nov 03 '11 at 13:47
  • The following questions may be relevant: http://stackoverflow.com/questions/8556203/onclick-javascript-playsound and http://stackoverflow.com/questions/4455282/call-a-javascript-function-at-a-specific-time-of-day. – Anderson Green Feb 12 '13 at 11:14

1 Answers1

0

It should be straightforward for javascript to play a sound - this jquery plugin should help: http://plugins.jquery.com/project/sound_plugin. The problem with what you want to do is the timing. Javascript will only run on page load, or when the user carries out a particular action such as moving their mouse over an element of a page or clicking a link. So although you could check the time of day at the time it runs, you'd have no guarantee that the user would carry out the necessary action such as loading the page at the moment you want the alarm to sound. You could have some kind of infinite script which repeatedly checks the time of day, with pauses between, but that would be horrible. You'd be far better off using one of the many reminder apps out there rather than trying to do this in javascript.

Brighty
  • 383
  • 2
  • 11
  • Well he would if he followed tge idea above of checking the time of day, pausing and then checking again. The script would need to run in a continuous loop. But that would be a horribly inelegant way to do it not to mention a performance hit. But let me know if I've missed an obvious way to do it using setTimeout. – Brighty Nov 03 '11 at 09:45
  • The whole point of setTimeout is delaying things *without* entering a busy loop. I am just not sure how reliable it is for long-term delays though. – hugomg Nov 03 '11 at 11:35
  • @Brightly, agreeing with missingno, it's possible to do a recursive time out, say he checks the time every 30 mins, shouldn't be too problematic – Abe Petrillo Nov 03 '11 at 13:11
  • Thanks missingo and Abe Petrillo that's interesting to learn. Still relies on the user having the relevant page open on their machine though. – Brighty Nov 04 '11 at 08:51
  • @missingno I've [already written a demo](http://jsfiddle.net/jarble/qnVy6/1/) that calls a function at specific times of day. Now all I need to do is make it play a sound play every 3 seconds, instead of printing a message every 3 seconds. Then I'd have a complete solution to this question. :) – Anderson Green Feb 12 '13 at 11:31