[CODE] cool elapsed function

Andre Garzia andre at andregarzia.com
Mon Dec 17 11:46:07 EST 2012


Folks,

I've been using a little function that I liked very much so I decided to
share.

the function is called elapsed and you can use it to figure out if a
certain amount of time passed between two timestamps. For example, suppose
you have variable time1 with a given time and variable time2 with another
point in time and you want to know if the elapsed time between time1 and
time2 is larger than one hour and half, you can use:

if elapsed(time1, time2, "1:30:00") then
  -- more than 1 hour and a half.
else
  -- less or equal 1 hour and a half.
end if

The cool thing is the third parameter which is a string in the format of
hh:mm:ss, so if you want to see if 10 seconds have passed between two
timestamps you can use:

if elapsed(time1, time2, "::10") then
...
end if

I use this type of code to prevent some accidental clicks when scrolling on
mobile. I record scrollerDidScroll timestamps and compare them with my
mouseUp timestamps, if they are too close, then I consider it an accidental
click.

Here goes the function:

function elapsed pTimeStart, pTimeEnd, pThreshold

   if pTimeStart is not a number then
      convert pTimeStart to seconds
   end if

   if pTimeEnd is not a number then
      convert pTimeEnd to seconds
   end if

   set the itemdel to ":"
   if item 1 of pThreshold is a number then
      put item 1 of pThreshold into tHours
   else
      put 0 into tHours
   end if

   if item 2 of pThreshold is a number then
      put item 2 of pThreshold into tMinutes
   else
      put 0 into tMinutes
   end if

   if item 3 of pThreshold is a number then
      put item 3 of pThreshold into tSeconds
   else
      put 0 into tSeconds
   end if

   put (tHours * 60 * 60) + (tMinutes * 60) + tSeconds into tThreshold

   put pTimeEnd - pTimeStart into tElapsed
   waveLog "elapsed check:" && pTimeStart && pTimeEnd && tElapsed &&
tThreshold
   if tElapsed > tThreshold then
      return true
   else
      return false
   end if
end elapsed

Cheers
andre

-- 
http://www.andregarzia.com -- All We Do Is Code.
http://fon.nu -- minimalist url shortening service.



More information about the use-livecode mailing list