Jump to content

[SOLVED] Adding a timer to a loop


Adergon
 Share

Recommended Posts

  • Moderators

Adergon,

Look at TimerInit and TimerDiff. Then you can code something like this:

$iBegin = TimerInit()

While TimerDiff($iBegin) < whatever_delay_you_want
    Your_Code
WEnd

; You get here when the delay ends

All clear? :)

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

Adergon,

Look at TimerInit and TimerDiff. Then you can code something like this:

$iBegin = TimerInit()

While TimerDiff($iBegin) < whatever_delay_you_want
    Your_Code
WEnd

; You get here when the delay ends

All clear? :)

M23

This probably works, but i don't know where to put the delay? do you mean like this?

While TimerDiff($iBegin) < 10000

Delay for 10 secs.

Link to comment
Share on other sites

  • Moderators

Adergon,

Have you tried? :)

Yes!

;)

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

Adergon,

Have you tried? ;)

Yes!

:D

M23

$iBegin = TimerInit()
While TimerDiff($iBegin) < 10000
Do
$TestPixel=PixelSearch(302, 26, 817, 359, $PixelTest, 5)
Until Not @error
MouseMove(0,0)
WEnd
MouseMove(15,3)

I tried this, but still, after 10 seconds. it doesnt move on. it still keeps trying to find it :)

EDIT: i added a mousemove and the end too so i know it has moved on:P

Edited by Adergon
Link to comment
Share on other sites

  • Moderators

Adergon,

You need to reverse the comparison if you use a Do...Until loop: :)

$iBegin = TimerInit()
Do
    $TestPixel = PixelSearch(302, 26, 817, 359, $PixelTest, 5)
Until (Not @error) Or TimerDiff($iBegin) > 10000

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

Adergon,

You need to reverse the comparison if you use a Do...Until loop: :)

$iBegin = TimerInit()
Do
    $TestPixel = PixelSearch(302, 26, 817, 359, $PixelTest, 5)
Until (Not @error) Or TimerDiff($iBegin) > 10000

M23

Thank you Melba, it worked. But if im right, this command waits 10 seconds, and then finds the color, and stops. But i want it to search for the color, for 10 seconds and then stop if it doesn't find it. and then move on to next function.

ThANks!

Link to comment
Share on other sites

  • Moderators

Adergon,

I am afraid you are wrong! :D

Let us run through the code:

- 1. We set a timestamp with TimerInit.

- 2. We enter a Do loop

- 3. We search for a colour with PixelSearch

- 4. We now check the Until line:

- 4A. If we find the colour @error = 0 so (Not @error) = 1 and we exit the loop (remember that AutoIt treats 0 as False and 1 as True) If we did not find the colour then @error = 1 so (Not @error) = 0 and we move on to check the comparison after the Or....

- 4B. Here we use TimerDiff to see if more than 10secs have elapsed. If they have then we exit the loop - if not then neither part of the Until checks are true and so we loop again.

All clear now? :)

M23

P.S. I would add a Sleep(10) in that loop or your CPU is going to get a bit hot with an almost permanent search. ;)

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

Adergon,

I am afraid you are wrong! :D

Let us run through the code:

- 1. We set a timestamp with TimerInit.

- 2. We enter a Do loop

- 3. We search for a colour with PixelSearch

- 4. We now check the Until line:

- 4A. If we find the colour @error = 0 so (Not @error) = 1 and we exit the loop (remember that AutoIt treats 0 as False and 1 as True) If we did not find the colour then @error = 1 so (Not @error) = 0 and we move on to check the comparison after the Or....

- 4B. Here we use TimerDiff to see if more than 10secs have elapsed. If they have then we exit the loop - if not then neither part of the Until checks are true and so we loop again.

All clear now? :)

M23

P.S. I would add a Sleep(10) in that loop or your CPU is going to get a bit hot with an almost permanent search. ;)

Sorry for wasting your time if you need to go, but hmm.. so this code:

$iBegin = TimerInit()
Do
$TestPixel=PixelSearch(302, 26, 817, 359, 0xB9910F, 5)
Until (Not @error) Or TimerDiff($iBegin) > 10000
MouseMove($TestPixel [0], $TestPixel [1])
MouseMove(15,3)

It searches for the color, if it doesn't find it within 10 secs. it exits the loop and moves on to the next function/line. right? but i get an error in this code:

Subscript used with non-Array variable. (Line 17)

Edit: the last mousemove in end is to see if it exits the loops and moves on. ;P

Edited by Adergon
Link to comment
Share on other sites

  • Moderators

Adergon,

You exit the loop on finding the colour or if the timer ran out. In the second case you will not have a valid coordinate array so you need to check if you have an array before trying to use it like this:

$iBegin = TimerInit()
$TestPixel = 0 ; Force the variable NOT to be an array
Do
    $TestPixel = PixelSearch(302, 26, 817, 359, 0xB9910F, 5)
Until (Not @error) Or TimerDiff($iBegin) > 10000
If IsArray($TestPixel) Then ; Check if have a valid array now
    MouseMove($TestPixel[0], $TestPixel[1])
EndIf
MouseMove(15, 3)

Got it? :)

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

Adergon,

You exit the loop on finding the colour or if the timer ran out. In the second case you will not have a valid coordinate array so you need to check if you have an array before trying to use it like this:

$iBegin = TimerInit()
$TestPixel = 0 ; Force the variable NOT to be an array
Do
    $TestPixel = PixelSearch(302, 26, 817, 359, 0xB9910F, 5)
Until (Not @error) Or TimerDiff($iBegin) > 10000
If IsArray($TestPixel) Then ; Check if have a valid array now
    MouseMove($TestPixel[0], $TestPixel[1])
EndIf
MouseMove(15, 3)

Got it? :)

M23

Melba is there anyway i can pay you?!?! omg thank you so much!!!! <33 ;):D:D:D
Link to comment
Share on other sites

  • Moderators

Adergon,

Melba is there anyway i can pay you?!?!

You could perhaps donate something to help Jon pay the hosting costs - every little helps. :)

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

  • Moderators

Adergon,

We do not "close" threads - unless you do something we do not like! ;)

If you want to add [solved] or similar to the title, just edit the first post and select the "Use Full Editor" option - that allows you to edit the title as well. :)

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

Please do not remove the title and text of the original post when solved! You take away important information for later reference.

All the replies are meaningless without your question!

Add [solved] at beginning of the title - that's all. This tells other users that there is a solution to the problem and might help them solve their own problem.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

Title reset to something close to the original. ;)

Adergon,

Please do not do this again - just add [sOLVED] to the title, not replace it. :)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...