Jump to content


Photo

[function] Sleep() in seconds, minutes, hours or days


  • Please log in to reply
7 replies to this topic

#1 Learning one

Learning one
  • Members
  • 1295 posts

Posted 13 November 2010 - 03:22 PM

Sleep in seconds, minutes, hours or days. Very short and simple. No more calculations like how many miliseconds are in 2 hours?
Related: Sleep Until Time by None
Sleep(n,u) { ; Sleep in seconds, minutes, hours or days. By Learning one, SKAN and MasterFocus.
	Sleep, % ( n*1000* (((u:=SubStr(u,1,1))="s") ? 1 : (u="m") ? 60 : (u="h") ? 3600 : (u="d") ? 86400 : 0 ))
}

/*
; Examples:
Sleep(3, "seconds") 	; (seconds,second,sec,s --> all the same)
Sleep(1, "min")			; (minutes,minute,min,m --> all the same)
Sleep(1, "h")			; (hours,hour,h --> all the same)
Sleep(0.5, "day")		; (days,day,d --> all the same)
*/
Original version:
Sleep(number,unit) {	; Sleep in seconds, minutes, hours or days. By Learning one.
	Static u := "s1000|m60000|h3600000|d86400000"
	Loop, parse, u, |
	{
		if (SubStr(A_LoopField,1,1) = SubStr(unit,1,1))
		Sleep, % number*SubStr(A_LoopField,2)
	}
}


#2 poetbox

poetbox
  • Members
  • 107 posts

Posted 18 November 2010 - 01:15 PM

good.thank you.

#3 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 18 November 2010 - 03:14 PM

Ternary based...

Sleep( n,u ) {
 Sleep, % SubStr( u := SubStr( u,1,1 ), 1, 0 ) . ( n * ( (u="s") ? 1000 : (u="m") ? 60000
        : (u="h") ? 3600000 : (u="d") ? 86400000 : 0 ))
}


#4 Delusion

Delusion
  • Members
  • 272 posts

Posted 18 November 2010 - 05:27 PM

very usefull thank you!

#5 MasterFocus

MasterFocus
  • Moderators
  • 4128 posts

Posted 19 November 2010 - 03:31 AM

@SKAN: Wouldn't the inner SubStr be enough?
Sleep( n,u ) {
 Sleep, % ( n*[color=red]1000*[/color] ( ([color=red](u:=SubStr(u,1,1))[/color]="s") ? [color=red]1[/color] : (u="m") ? [color=red]60[/color]
        : (u="h") ? [color=red]3600[/color] : (u="d") ? [color=red]86400[/color] : 0 ))
}


#6 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 19 November 2010 - 07:26 AM

@MasterFocus: Neat!.. I love it :D

#7 fincs

fincs
  • Fellows
  • 1530 posts

Posted 19 November 2010 - 04:24 PM

Here's my take:
Sleep(n, u="s")
{
	Sleep % n * ((u := SubStr(u, 1, 1)) = "s" ? 1000
	            : u = "m" ? 60000
	            : u = "h" ? 3600000
	            : u = "d" ? 86400000
	            : 0)
}
Just because it is compact doesn't mean it has to be unreadable.

#8 Learning one

Learning one
  • Members
  • 1295 posts

Posted 19 November 2010 - 06:03 PM

1. post updated with short version.

P.S. maybe suggestion for Sleep command in AHK_L:
Sleep, delay [, unit]