Jump to content

while loop


Recommended Posts

so right now i have a small project and i wanted to print this text out every second.

i have made a random number for the sleep function. but when i want to print out with the while loop it just doesn't work.

Run("notepad.exe")
Sleep(1000)
Local $rndSleep = Int (Random(180000,240000,1000))

While $rndSleep <> 0
   $rndSleep - 1
   If Mod ( $rndSleep, 1000 ) == 0 Then
   Send("This note will show the sleeptime before closing the tabs, you got " & $rndSleep & " seconds left.")
   EndIf
WEnd

plis halp me :D

Link to comment
Share on other sites

  • Moderators

ZeroByDevide,

This works for me:

Local $rndSleep = Int  (Random(180,240,1))

$iSec = @SEC

While 1
    If $iSec <> @SEC Then
        $iSec = @SEC
        $rndSleep -= 1
        If $rndSleep Then
            ConsoleWrite("This note will show the sleeptime before closing the tabs, you got " & $rndSleep & " seconds left." & @CRLF)
        Else
            ExitLoop
        EndIf
    EndIf
    ; Important to let the CPU breathe
    Sleep(10)
WEnd

M23

Edited by Melba23
Amended code

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

There are  few errors in the original code:

; this won't do anything
$rndSleep - 1

;you need
$rndSleep -= 1

This might be of help:
https://www.autoitscript.com/autoit3/docs/intro/lang_operators.htm

;this will work
If Mod ( $rndSleep, 1000 ) == 0 Then

;but you should use
If Mod ( $rndSleep, 1000 ) = 0 Then
;as == is for comparing strings, so there is extra work being done to convert both sides of the comparison to strings

Other problems with the code are:

1) It won't display the text every second, it will display every 1000 loops of the code, which will depend on the CPU speed.

2) There's no carriage return, so the text will display all on the same (long) line. Add & @CRLF  to the send command.

3) If you click on a different window, that window will receive the text, this could cause problems :D

Melbas solution has a problem, it doesn't stop at 0

Link to comment
Share on other sites

  • Moderators

weirddave,

Quote

Melbas solution has a problem, it doesn't stop at 0

It does now - thanks for the prompt!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...