ZeroByDevide

while loop

5 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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
1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

THanks!!

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

weirddave,

Quote

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

It does now - thanks for the prompt!

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
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

  • Similar Content

    • InunoTaishou
      By InunoTaishou
      I know on other languages when you try to use a string like this
      Global $sString = "Some String" If ($sString) Then ; ... EndIf The compiler will fail because string cannot be used in an expression where it needs a bool (something along those lines). But I see instances in other languages where using $sString in an expression will work and implicitly use the length of the string (or because there is some value in that variable).
      I'm wondering why AutoIt doesn't do the same. If the $sString is used like in my example (or some other expression where it wants a bool type) implicitly use the length of the string instead of the converting to 0?
    • AlexFing17
      By AlexFing17
      Hello, i am creating  a program that 
      1. it will check if the computer is connected to the internet 
      2. will display a message if it is connected 
      3.if not connected then it will continue to check until the computer connects to the internet then it runs the function which would have been executed if the computer was connected to the internet.
      what i tried.
      i connected my computer to the internet and run the code. Which displayed the msgbox that am connected
      i later turn my internet off and run the cold , i saw it running in process list.. i waited for 5 minutes and it was still waiting for internet connection (running), but when i turned the internet on and got internet connectivity, the app just close without displaying the msg that am connected.

      Please need help where i went wrong. or if anything should be added.
       
      this is the code
      #AutoIt3Wrapper_icon=1.ico $ping = Ping("www.google.com") If Not @error Then MsgBox("", "AlexFing", "You are connect") Else Do $ping = Ping("www.google.com") Until Not @error EndIf  
    • Miliardsto
      By Miliardsto
      Is there possibility to do it like this?
      Cause as I see compiler doesn't see If statement in case of #include functions.
      It includes these two files all time.
      If $var == True Then #include 'func/funcsByTabs/main_Moda.au3' Else #include 'func/funcsByTabs/main_Others.au3' EndIf  
    • Warriorfoe
      By Warriorfoe
      Okay so here's the problem.
      I have a script I'm testing out where I want it to search for a certain color and if it finds that certain color I want it to check for a second color. I only want it to check for the second color after checking for the first color. If it finds the first color it will check for the second color and whether or not that second color is present it will cause certain actions to be undertaken. Well at least that's what I want it to do. I want this all to be done in a loop where it's constantly checking for the first color. However, once it does find the first color and it executes one of the actions regarding the second, I want the program to quit. I'm having trouble figuring out how to nest conditional statements here. If anyone could help me out it would be much appreciated.
      HotKeySet("{ESC}", "Terminate") HotKeySet("!c", "getColor") HotKeySet("!l", "ClickPositions") Global $color Global $interval $interval = "10000" MsgBox (0, "Starting Up", "Starting, use ESC to quit, Alt + C to get color, and Alt + L to begin Test") Func Terminate() Exit 1 EndFunc Func ClickPositions() MsgBox(0, "Beginning Test", "Hope to god this works") While 1 $Coords = PixelSearch(1006, 48, 1074, 79, 0xECECEC) $1stcoords = PixelSearch(930, 621, 1066, 649, 0x8A211E) $point = MouseGetPos() $posx = Random(930, 1066) $posy = Random(621, 649) $clicklogx = Random(1076, 1093) $clicklogy = Random(38, 54) If IsArray($Coords) Then ;check to see if the first color is there If IsArray($1stcoords) Then ;if it is check for the second color and if that one is there: MouseClick( "left", $point[0], $point[1], 1) If IsArray(<>$1stcoords) Then ;if the second color is not there: MouseClick("left", $clicklogx, $clicklogy, 1, Random(1000, 2500)) Sleep(3000) MouseClick( "left" , $posx, $posy, 1, Random ( 1000 , 2500)) EndIf EndIf EndIf Sleep(2000) WEnd EndFunc While 1 Sleep(250) WEnd