Author Topic: Delay loop  (Read 472 times)

0 Members and 1 Guest are viewing this topic.

bosmart

  • Mosquito
  • Posts: 2
Delay loop
« on: January 30, 2024, 10:15:39 AM »
;Was looking for a dealy loop here, found some code but it was difficult so i made this one.
;Now it works fine and easy, am i wrong?
;Here is the code:

Code - Auto/Visual Lisp: [Select]
  1. ;;;######################################
  2. ;;;         ------- SubRutin msDealy -------
  3. ;;;         -------  dealy in millisec  -------
  4. ;;;        for one sec. delay call ==> (msDelay 1000)
  5. ;;;######################################
  6. ;----------------------------------------------------------------------
  7. (defun msDelay (ms);dealy in ms
  8.    (setq stime (getvar "millisecs"))
  9.    (setq etime (+ stime ms))
  10.    (while (< (getvar "millisecs") etime) )
  11. );defun msDelay
  12. ;-----------------------------------------------------------------------


EDIT (John): Added code tags.
« Last Edit: January 30, 2024, 10:26:06 AM by JohnK »

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: Delay loop
« Reply #1 on: January 30, 2024, 10:34:06 AM »
Other than localizing your variables, it seems to work for me.

However, if you want to get around localizing your variables, you could build the routine like this (same code, just refactored a bit to use the let construct).
Code - Auto/Visual Lisp: [Select]
  1. (defun msDelay (ms)
  2.   (
  3.    (lambda (stime)
  4.      (while (< (getvar "millisecs") (+ stime ms))))
  5.    (getvar "millisecs")))
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: Delay loop
« Reply #2 on: January 30, 2024, 02:20:33 PM »
;Now it works fine and easy, am i wrong?
for short delays it's ok
but for longer ones i'd use buil-in "_delay" command

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Delay loop
« Reply #3 on: January 30, 2024, 07:07:40 PM »
I use the built-in DELAY command as this results in an idle CPU without the application being marked as non-responsive by the OS after 5 seconds.

bosmart

  • Mosquito
  • Posts: 2
Delay loop
« Reply #4 on: January 31, 2024, 12:57:06 PM »
JohnK

Nice and short, but i am a newbe so lamda it still confusing me.
/Maarten

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: Delay loop
« Reply #5 on: January 31, 2024, 02:25:25 PM »
JohnK

Nice and short, but i am a newbe so lamda it still confusing me.
/Maarten

Maarten,
Lambda is easy. That is to say:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( x ) ...
is the same--but without a name--as:
Code - Auto/Visual Lisp: [Select]
  1. (lambda ( x ) ...

Code - Auto/Visual Lisp: [Select]
  1. (
  2.  (lambda ( argument ) ... )
  3.  x
  4.  )
is the LET construct (-i.e. use a `lambda` statement to make an assignment instead of using `setq`). See: https://www.theswamp.org/index.php?topic=58202.msg613941#msg613941
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org