AutoHotkey Community

It is currently May 27th, 2012, 2:55 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Timers
PostPosted: November 29th, 2009, 3:37 am 
Offline

Joined: March 22nd, 2009, 11:12 pm
Posts: 28
SetTimer has a limitation which I would like to see added - support for multiple timers. I propose:
Code:
SetTimer, Label [, Period, TimerNumber]

This allows you to create separate timers, for instance:
Code:
indexb := indexu := 1
return
#IfWinActive, IRCclient
!b::
Input, name%indexb%,, {Enter}
SendInput, % "/mode #example +b " . name%indexb% . "{Enter}"
SendInput, % "/kick #example " . name%indexb% . "{Enter}"
SetTimer, unban, -60000
indexb++
Return
unban::
SendInput, % "^a^x/mode #example -b " . name%indexu% . "{Enter}"
SendInput, % "/invite " . name%indexu% . " #example{Enter}"
SendInput, % "/privmsg " . name%indexu% . " You can come in now.{Enter}^v"
indexu++
Return
#IfWinActive

It's a ban-for-a-minute IRC thing which unfortunately fails because of the timer issue. The version with SetTimer's extra parameter:
Code:
indexb := indexu := 1
return
#IfWinActive, IRCclient
!b::
Input, name%indexb%,, {Enter}
SendInput, % "/mode #example +b " . name%indexb% . "{Enter}"
SendInput, % "/kick #example " . name%indexb% . "{Enter}"
SetTimer, unban, -60000, %indexb%
indexb++
Return
unban::
SendInput, % "^a^x/mode #example -b " . name%indexu% . "{Enter}"
SendInput, % "/invite " . name%indexu% . " #example{Enter}"
SendInput, % "/privmsg " . name%indexu% . " You can come in now.{Enter}^v"
indexu++
Return
#IfWinActive

This would work, because the timers wouldn't interrupt each other. A workaround for two timers per subroutine (this doesn't work right, I haven't tinkered with it enough, but in any case, I need more than 2 timers):
Code:
temp := 0
!a::
SetTimer, SubRoutine, -60000
SetTimer, gettime, 100
Return
!b::
SetTimer, gettime, off
temporary := temp
temp := 0
Return
SubRoutine:
; Insert normal function here
If temp != 0
 SetTimer, SubRoutine, % temporary
Return
gettime:
temp -= 100
Return


Please either give your support so we can get this in the next release, or show how it can currently be done!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 29th, 2009, 10:53 am 
Offline

Joined: August 24th, 2005, 5:29 pm
Posts: 549
Location: Berlin / Germany
You might add the name and the current time to a list, which is checked by a timer at regular intervals. If 60 seconds are elapsed, send an invitation and remove the entry from the list.

_________________
nick :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 30th, 2009, 9:21 pm 
Offline

Joined: March 22nd, 2009, 11:12 pm
Posts: 28
nick wrote:
You might add the name and the current time to a list, which is checked by a timer at regular intervals. If 60 seconds are elapsed, send an invitation and remove the entry from the list.


Good idea! Here's an untested function (on a Mac, ew. only calls each once, you have to reset it inside the subroutine. not very accurate.):
Code:
SetTimer(Label, Period)
{
 Period := Round(Period, -2)
 List .= Label . " " . Period . "|"
 SetTimer, CheckSetTimer, 100
 Return
 CheckSetTimer:
 Loop, parse, List, |
 {
  StringSplit, CST, A_LoopField, %A_Space%
  CST2 -= 100
  If (CST2 = 0)
  {
   Gosub, %CST1%
   StringReplace, List, List, % A_LoopField . "|"
 }
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 1st, 2009, 2:27 pm 
Offline

Joined: January 2nd, 2008, 4:47 am
Posts: 150
Location: Freenode IRC
Original Poster:

You may not be aware, but the "Label" parameter is actually the timer's name. You can have as many timers as you want, each with a different label.

You want multiple timers to call the same sub-routine label? Easy!

Code:
#SingleInstance, Force
#Persistent
#NoEnv

SetTimer, Label1, 1000     ; Every second
SetTimer, Label2, 3600000  ; Every hour

RETURN ; End of Auto-execute section.

Label1:
; This falls through into Label2
Label2:
  ; Stuff that happens when either timer fires.
Return


And you're done!

_________________
Image

Need help right away? Get live support on IRC.
Already have an IRC client installed? /join #ahk


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 22nd, 2009, 4:41 am 
Offline

Joined: March 22nd, 2009, 11:12 pm
Posts: 28
Good idea, Raccoon! Unfortunately, that doesn't let you use an infinite number of timers, unless you have an infinite number of label declarations. If you don't, then you'll eventually run out.

EDIT: I said that wrong. This topic is about setting multiple timers on a single label. If you use Label1, Label2, etc, you are limited to the number of labels you wrote. I suppose you could have 100 labels and an array stating whether each was free, though. That's probably all I'd need, but a fourth parameter would be even better.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group