Page 1 of 1

AddScreenTimer

Posted: Sat Oct 10, 2020 6:55 pm
by plouf
currently timers can be created ON LY if you have a window,, but in many case you never open window (games etc)

additionally a "delay()" would be nice
i know javascript has not but spider can simulate it with settimeout internally and give to high level (basic level) a Delay

Re: AddScreenTimer

Posted: Sun Oct 11, 2020 6:57 am
by Kurzer
Hello plouf,

SpiderBasic seems to store the data for the timer on the window so I don't know if this can be detached from the window at all.

The following is a dangerous and maybe wrong hack. I have little to no knowledge of JavaScript and have extracted the necessary information for this hack from the compiled file "spiderbasic.js".

Creating a timer without a window seems to work (although I don't know if the hack messes up anything internally). But a RemoveTimer does not work like this. I was not able to adjust the JavaScript parameters of the RemoveTimer() function, so that they work without a window. Maybe the Chuck Norris of JavaScript (Peter) can help? ;-)

RemoveTimer() in JavaScript: function(b,a){var c;if(c=spider.window.a.Get(b)){var e=""+a;c.d[e]&&(clearInterval(c.d[e]),delete c.d[e])}}

Code: Select all

; Timerevent without window

Procedure TimerEvents()
	Debug "Timer event: " + EventTimer()
EndProcedure

Global.i timerno  = 0, interval = 1000

; 12 = #PB_Event_Timer
; -65535 = #PB_Ignore
; Add the timer
! setInterval(function(){spider.event.Send(12,-65535,v_timerno,0)},v_interval)

BindEvent(#PB_Event_Timer, @TimerEvents())

; This does not work (RemoveTimer)
; ! (clearInterval(v_timerno), delete v_timerno)
Maybe this will help you as a workaround.

Markus

Re: AddScreenTimer

Posted: Sun Oct 11, 2020 7:08 am
by plouf
HI
thanks for your time

maybe a safer workaround for now is to create a window with #PB_Window_Invisible

but in general this problem must addressed for future needs, i.e. create some global timers / screen timers etc

Re: AddScreenTimer

Posted: Sun Oct 11, 2020 1:52 pm
by Peter
How about this?

Code: Select all

Procedure AddTimer(Timeout, Callback)
  ! return setInterval(v_callback, v_timeout);
EndProcedure

Procedure RemoveTimer(Timer)
  ! clearInterval(v_timer);
EndProcedure

Procedure TimerEvent()
  Debug ElapsedMilliseconds()
EndProcedure
  
myTimer = AddTimer(1000, @TimerEvent())

; RemoveTimer(myTimer)

Re: AddScreenTimer

Posted: Sun Oct 11, 2020 3:07 pm
by plouf
guys thanks for your time
both your codes prove that it is possible

However keep in mind that is posted in "Feature Request and Wishlist" section, and for reason explained i believe these commands should supported natively ! :)