Permanent display of a time

Hello
I’m brand new here in the forum, maybe someone can help me with my first small program, which is only supposed to display a clock, unfortunately it doesn’t work.
The time is only displayed when I click, not constantly. It doesn’t work in a Do/Loop loop either.
I see very few posts here, am I actually in the right place with my question?

Dim Zeit,Sek
Function Button1_onclick()
Zeit = FormatDateTime(Now,vbShortTime)
TextArea1.value="Datum: "&FormatDateTime(Now, [“DD.MM.YY”]) & vbCRLF
Sek=Second(Now)
If Len(Sek)=1 Then Sek=“0” & Second(Now)
TextArea1.value= TextArea1.value & "Zeit: "& Zeit & “:” & Sek & vbCRLF
Time1.value = Zeit & “:” & Second(Now)
End Function

The UI needs a chance to redraw the value in TextArea1. A click event is perfect. The UI is redrawn after your function returns.

But if you use a loop, the UI is never redrawn.

I’m not a BASIC programmer, but I think you’ll need to use an interrupt event, like setTimeout or setInterval in JavaScript.

setTimeout or setInterval are both usable in BASIC too - check with Wiki.

@Marcuse,

Your function might look something like this:

Sub UpdateTime()
    textarea.Text = Time()
    timerId = SetTimeout(UpdateTime, 1000)
End Sub

Hello
Thanks for the quick help, it works now, but I can’t play an MP3 file, it works with the button function. But not with the string, I don’t understand why.

Dim Zeit,Sek,timerRef,Zeit_L
timerRef = SetInterval(nextAction, 1000)
Function nextAction()
    Zeit = FormatDateTime(Now,vbShortTime)
    TextArea1.value="Datum: "&FormatDateTime(Now, ["DD.MM.YY"]) & vbCRLF
    Sek=Second(Now)
    If Len(Sek)=1 Then Sek="0" & Second(Now)
    Zeit_L=Zeit & ":" & Sek
    TextArea1.value= TextArea1.value & "Zeit: "& Zeit_L & vbCRLF
    If Zeit_L ="21:19:00" Then Call to_do
End Function
  
Function Button1_onclick() 
  Call  to_do
End Function

Function to_do
 ClearInterval(timerRef) 
 Audio1.play()
 Print "XX"
End Function

Tip : If you’re pasting code, html or config files, surround the code with triple back ticks (```), before the first line and after the last one. It will be formatted properly. (We fixed it for you this time)

Browsers won’t play sound without having had user interaction.

This may be inconvenient for, but it prevents badly behaved sites from making sound before you have a chance to stop them.

Hello
Thanks for the info, is it possible to work around this, e.g. for a simple alarm clock with mp3?

The only solution I’ve seen here in the topic looks something like this, it works, but I have to activate the alarm manually.
Does anyone else have an idea without a button?

Dim Zeit,Sek,timerRef,Zeit_L
timerRef = SetInterval(nextAction, 1000)
Function nextAction()
    Zeit = FormatDateTime(Now,vbShortTime)
    TextArea1.value="Datum: "&FormatDateTime(Now, ["DD.MM.YY"]) & vbCRLF
    Sek=Second(Now)
    If Len(Sek)=1 Then Sek="0" & Second(Now)
    Zeit_L=Zeit & ":" & Sek
    TextArea1.value= TextArea1.value & "Zeit: "& Zeit_L & vbCRLF
    If Zeit_L ="23:45:00" Then Call To_do
End Function
 
Function To_do 
Audio1.play()
Audio1.loop = False
End Function
Function Button2_onclick() 
'Aktivate Alarm Clock
Button2.value ="Alarm aktiv"
Audio1.play()
Audio1.pause()
End Function

This is in Java, but it works. You can probably figure it out for vb
The audio plays when the seconds equal 10 sec!! Keeps playing till I hit the stop button, but the clock “keeps on ticking”!

Paul

let t_time;
let t_time_second;

Button3.onclick = function(){
  var myVar = setInterval(function() {
   t_time = new Date().toLocaleTimeString('en-US', { hour12: false, 
                                             hour: "numeric", 
                                             minute: "numeric",
                                             second: "numeric"});
   t_time_second = new Date().toLocaleTimeString('en-US', { hour12: false, 
                                             second: "numeric"}); 
    Textarea1.value = t_time;
    
    if(t_time_second == "10"){
           audio.play();
      }
  }, 1000);

Button4.onclick = function(){
      audio.pause();
}

Looking at your code… I think you need some parentheses in some of your code. You might try:

Dim Zeit,Sek,timerRef,Zeit_L
timerRef = SetInterval(nextAction, 1000)
Function nextAction()
    Zeit = FormatDateTime(Now,vbShortTime)
    TextArea1.value="Datum: "&FormatDateTime(Now, ["DD.MM.YY"]) & vbCRLF
    Sek=Second(Now)
    If Len(Sek)=1 Then Sek="0" & Second(Now)
    Zeit_L=Zeit & ":" & Sek
    TextArea1.value= TextArea1.value & "Zeit: "& Zeit_L & vbCRLF
    If Zeit_L ="23:45:00" Then Call To_do()
End Function
 
Function To_do() 
Audio1.play()
Audio1.loop = False
End Function

Function Button2_onclick() 
'Aktivate Alarm Clock
Audio1.play()
Button2.value ="Alarm aktiv"
End Function

Getting Audio or Video to play works for me programmatically for alarms using the Multimedia controls for Audio and Video. I had a couple of 7" tablets not being used and my clock died so I decided to use one of the unused tablets for a clock with alarm. I use the audio, video and .gifs to act as alarms with user settings.

A tricky problem was to keep the clock running at full screen brightness at all times. The Android OS wants to dim the screen if the device is not being used. Settings in the OS do not have a “never” for dim timeout. I was able to get this to not dim by putting a video control on the main form and set it running at start up, but hidden off screen. The .mp4 video is in a continuous loop and is started when you select the red “Start” button after the app has started. This fools the Android OS into leaving the brightness alone.

This is a PWA and works on Android Tablets, Phones, and Windows, but not tried on an iOS device. I had this clock running on an older Samsung tablet for over a year and only occasionally did something happen to cause it to exit and show the Home screen. Maybe an OS update. This Tablet finally gave up working just today, and guess what, I have another Tablet not being used so it is now keeping time for me. I display the time filling the whole landscape form in bright red numbers. Easy for my tired eyes to see across the room.

To see this app run: https://prograsa.com/myAlarm2/index.html

John

I have an app, Javascript, which can play mp3 files on click. It uses

const AudioContext = window.AudioContext || window.webkitAudioContext;
const context = new AudioContext({
  latencyHint: 'interactive',
  sampleRate: 44100,
});

If interested in details I can send the complete JavaScript Code. Some comments are in German.
You can find the app at
``

Available and working as a WebApp and as a native App (download apk).

Pretty cool, John. Any chance of getting this code? I’m working on an app where I want to link several mp3 files together so one can cord along with a song. each mp3 file will be a different chord on the guitar. You app might give me some good ideas.
Paul

pwleblanc506@yahoo.com

Hello
Yes, it’s a good idea to let an MP4 film run endlessly to keep the screen active, I’ll have to test that, but my problem right now is letting the program run in the background when the screen is switched off.

Falcon:

There is a lot more in this app than meets the eye, and a bit more than I have time to explain. But to help you get the basics of playing an mp3 or mp4 based on a set time hopefully this will help:

All mp3s and mp4s are in extra files in my clock app, although you could probably have them on your server as well. Variables need to be defined at start. When the user stops the alarm (screen click or something) then issue Audio1.pause or Video1.pause and .hide these controls.

The user must select a “Start” button to play the diversion video which prevents screen dimming while the app is running. I do this using a start form with just one control with the word “Start” (lblBlackStart).

I cut, pasted, and removed a bunch of code from my app for the below sample code. It could be buggy:

Function lblBlackStart_onclick()
  frmAlarm.show()            'this form contains the clock and other objects
  lblBlackStart.hide()       'this is the first form 
  Call lblTime_onclick()
End Function

Function lblTime_onclick()
  tc = SetInterval(TimeClock,500) 'check every .2 seconds to reset the time and date and test for alarm
  Call StartMe()
End Function

Function StartMe()
  'necessary to prevent the OS from dimming the screen
  Video1.src = "drips.mp4" 'use any video, not too big, include in extraFiles
  Video1.show()
  Video1.play()
  Video1.style.left = "-1000px" 'this hides the video from the user, keeps screen from refreshing
End Function

Function TimeClock()
  'Reset the visual display labels of the time (lblTime) and date (lblDate)
  'and test for a time to make the alarm sound/show  
  C = FormatDateTime(Date, vbLongDate)
  lblDate.innerHTML = "<center>" & C & "</center>" 'lblDate on frmAlarm
  ClockTime = FormatDateTime(Now)
  b = InStr(ClockTime, " ")
  If b <> 0 Then
    ClockTime = Mid(ClockTime,b+1)
    ClockTime = Left(ClockTime,5)
  End If  
  lblTime.innerHTML = "<center>" & ClockTime & "</center>"  'lblTime on frmAlarm
  'Test to see if an alarm needs to be sounded/shown 	
  'You will need add your own method to get a time when an 
  'alarm is to be played. In this code aTime is the time to
  'play the alarm.
  Call CurrentTime()
  'The format of CurTime and aTime must be the same.
  If CurTime = aTime Then
    RA = SetTimeout(resetalarm,65000) '1 minutes + 5 seconds minutes
    Call PlayAlarm()
    Exit For
  End If  
End Function

Function resetalarm()
  ClearTimeout(RA)
  InAlarm = False
End Function  

Function CurrentTime()
  ClockTime = FormatDateTime(Now)
  b = InStr(ClockTime, " ")
  If b <> 0 Then
    ClockTime = Mid(ClockTime,b+1)
    apTime= Right(ClockTime,2)
    Clockhm = Left(ClockTime,5)
  End If
  CurTime = Clockhm & " " & apTime
End Function

Function PlayAlarm()
  'define AlarmType somewhere in app
  If AlarmType = "Video" Then
    vidAlarm.show()
    vidAlarm.play()
  ElseIf AlarmType = "Audio" Then
    Audio1.src = mp3toPlay
	Audio1.play()
    DA = SetTimeout(FinishAlarm,200)
  End If 
End Function 

Function FinishAlarm()
  ClearTimeout(DA)
  frmTrans.show()
  frmAlarm.hide()
End Function   

Thank you very much, I’ll have to read this carefully…

@Marcuse,

Back to George’s original comment that the browser environment does not allow sound before a user interaction, many apps deal with this by having a mute/unmute toggle on screen and start the app muted mode. Since the user wants sound, they then click the toggle to switch to unmuted mode, and the mere action of that click will allow your sound to be heard.

That’s still not ideal for you I’m guessing, as it appears you’re writing some kind of alarm clock, and users who want an alarm assume there’s never a reason to mute it. So, as George says… oh well, that’s how it is these days.