Jump to content

how to use basic timers?


Recommended Posts

i'm still learning autoit, can anyone help me with my script?  so basically i want to press f5 refresh my chrome browser every 3mins without ruining the loop of my script.

something like this


 

HotKeySet ("{HOME}", "Main")
HotKeySet ("{END}", "_Exit")

global $timer = 0

While 1
    Sleep(200)

WEnd



Func Main()
While 1

if $timer = 180000 then

send ("{f5}") ;refresh button

sleep(500)

global $timer = 0 ;reset timer to 0

else

  ;--my main script

   Mouseclick ("primary", 950,125,1,1)
   sleep(200)
    Mouseclick ("primary", 366,298,1,1)
    Sleep(2500)
    Mouseclick ("primary", 28,97,1,2)
   sleep(200)
   Mouseclick ("primary", 44,239,1,2)
   sleep(200)
    call ("showProfile")
    

Endif

Wend

Endfunc



Func _Exit()
Exit
EndFunc

 

Link to comment
Share on other sites

@wolfsnow11

Take a look at TimerInit() and TimerDiff() functions in the Help file.

Basically, TimerInit() "starts" counting, and TimerDiff(), from a given Timer handle (returned by TimerInit() function), returns the difference in milliseconds from when TimerInit() has been called.

Just take a look at them in the Help file; there are examples too :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Without seeing significantly more of your code, it is a bit hard to advise you beyond what @FrancescoDiMuro said.

You may or may not need to use the Timer functions, depending on how your code might be Looping ... so you might get away with a simple Sleep for 3 minutes ... though probably unlikely, especially if you have lots going on code wise.

Something else you will probably need to use is WinActivate, just prior each time, to ensure your browser window gets the refresh command.

And you will want a Hotkey somewhere in that loop to escape/exit .... or maybe better still, use the _IsPressed command.

It might help to know why you want your browser refreshed so often, as there may be a much better way to do things?

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

5 minutes ago, TheSaint said:

or maybe better still, use the _IsPressed command. 

I don't agree that it is better... much better if you use a global variable which can act as an indicator which you can check in every iteration of the loop. Use a function with HotKeySet and when pressed, activate the indicator by changing the global variable's value (can be a simple boolean like True) and the loop will exit the next time it checks the variable... much efficient than calling _IsPressed everytime.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

I kind of like the asynchronous aspect of the WinAPI timer personally

from the help file..

#include <Misc.au3>
#include <WinAPISys.au3>

Opt('TrayAutoPause', 0)

Local $hTimerProc = DllCallbackRegister('_TimerProc', 'none', 'hwnd;uint;uint_ptr;dword')
Global $g_iCount = 0

Local $iTimerID = _WinAPI_SetTimer(0, 0, 1000 * 60 * 3, DllCallbackGetPtr($hTimerProc))

Do
    Sleep(100)
Until _IsPressed('1B') ;ESC key..

_WinAPI_KillTimer(0, $iTimerID)
DllCallbackFree($hTimerProc)

Func _TimerProc($hWnd, $iMsg, $iTimerID, $iTime)
    #forceref $hWnd, $iMsg, $iTimerId, $iTime
    send ("{f5}") ;refresh button
    ConsoleWrite($g_iCount & @CRLF)
    $g_iCount += 1
EndFunc   ;==>_TimerProc

 

Edited by Bilgus
Link to comment
Share on other sites

AdlibRegister() is perfect for the job.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

2 hours ago, jchd said:

AdlibRegister() is perfect for the job.

will this gonna work? 

 

HotKeySet ("{HOME}", "Main")
HotKeySet ("{END}", "_Exit")

global $timer = 0

AdlibRegister("refresh", 1800000) ;30mins

While 1
    Sleep(200)
WEnd



Func refresh()
global $timer = 1
endfunc


Func Main()
While 1

if $timer = 1 then

send ("{f5}") ;refresh button

sleep(500)

global $timer = 0 ;reset timer to 0

else

  ;--my main script

   Mouseclick ("primary", 950,125,1,1)
   sleep(200)
    Mouseclick ("primary", 366,298,1,1)
    Sleep(2500)
    Mouseclick ("primary", 28,97,1,2)
   sleep(200)
   Mouseclick ("primary", 44,239,1,2)
   sleep(200)
    call ("showProfile")
    

Endif

Wend

Endfunc



Func _Exit()
Exit
EndFunc

 

Link to comment
Share on other sites

Don't redeclare the $timer variable, just set its value. Also don't use Global inside functions. Also, no where in your code are you changing the initial value of $timer so you aren't using it anywhere to time anything, at least in what you posted. 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

4 hours ago, BrewManNH said:

Don't redeclare the $timer variable, just set its value. Also don't use Global inside functions. Also, no where in your code are you changing the initial value of $timer so you aren't using it anywhere to time anything, at least in what you posted. 

how can i do that? can you pls make example thank you

Link to comment
Share on other sites

You'll have to be much more specific as to what you want. How can you do what?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

31 minutes ago, BrewManNH said:

You'll have to be much more specific as to what you want. How can you do what?

how about this? will this work?

HotKeySet ("{HOME}", "Main")
HotKeySet ("{END}", "_Exit")

global $timer = false

AdlibRegister("refresh", 1800000) ;30mins

While 1
    Sleep(200)
WEnd



Func refresh()
$timer = true
endfunc


Func Main()
While 1

if $timer = true then

send ("{f5}") ;refresh button

sleep(500)

$timer = false ;reset timer 

else

  ;--my main script

   Mouseclick ("primary", 950,125,1,1)
   sleep(200)
    Mouseclick ("primary", 366,298,1,1)
    Sleep(2500)
    Mouseclick ("primary", 28,97,1,2)
   sleep(200)
   Mouseclick ("primary", 44,239,1,2)
   sleep(200)
    call ("showProfile")
    

Endif

Wend

Endfunc



Func _Exit()
Exit
EndFunc

 

Link to comment
Share on other sites

  • Developers

What about stopping asking for solutions and checking code and first explaining what it is you want to accomplish, else simply use a sledgehammer. ;) 
..other than that the shown code is flawed, but we get to that when you have told us more information and provide runable code.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

18 hours ago, TheDcoder said:

I don't agree that it is better... much better if you use a global variable which can act as an indicator which you can check in every iteration of the loop. Use a function with HotKeySet and when pressed, activate the indicator by changing the global variable's value (can be a simple boolean like True) and the loop will exit the next time it checks the variable... much efficient than calling _IsPressed everytime.

I am not saying you are wrong, just that you need to consider the skill level of the OP .... where often easiest is best, until they know better ... coding is a journey you grow into in stages.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

5 hours ago, TheSaint said:

I am not saying you are wrong, just that you need to consider the skill level of the OP .... where often easiest is best, until they know better ... coding is a journey you grow into in stages.

I agree, _IsPressed might make it easier for them to understand it... however a simple example can go a long way:

HotKeySet("^b", StopLoop)

Global $bLoopActive = True

While True
    ; This is your loop that you want to exit
    Sleep(10) ; I am using the Sleep function as an example for the work that you want to do in a loop
    ; At the end of the loop, you can check if the loop has been stopped by the hotkey
    If Not $bLoopActive Then ExitLoop
WEnd

Func StopLoop()
    $bLoopActive = False
EndFunc

I wrote the above code in my browser and I did not test it, so please forgive any mistakes that I made in the example and correct them :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

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...