Execute function after some time

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm
Location: Croatia

Execute function after some time

Post by croivo » Fri Jan 22, 2016 11:14 pm

What is the most simple way to execute function after few seconds?
Let's say something like this: send myFunction(parameter) in 2 seconds
Thank you.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Execute function after some time

Post by Simon » Sat Jan 23, 2016 12:05 am

Hi croivo,
You are so very close!

Code: Select all

send "myFunction("& parameter &")" to me in 2 seconds
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm
Location: Croatia

Re: Execute function after some time

Post by croivo » Sat Jan 23, 2016 12:22 am

I've tried this:

Code: Select all

on mouseUp
   send "myFunction("& "Hello" &")" to me in 2 seconds
end mouseUp
   
function myFunction theText
   answer theText
end myFunction
and that doesn't work...

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Execute function after some time

Post by Simon » Sat Jan 23, 2016 12:39 am

OK you got me.
I'm sure there a better way to do this but here is my Hack;

Code: Select all

on mouseUp
   send timeFunction to me in 2 seconds
end mouseUp
   
on timeFunction
   return myFunction(Hello)
end timeFunction

function myFunction theText
   answer theText
end myFunction
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

FredBeck
Posts: 77
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: Execute function after some time

Post by FredBeck » Sat Jan 23, 2016 2:15 am

Hi!
That's because functions need to be GOT
You need either to :

send "get myFunction("& "Hello" &")" to me in 2 seconds

or

send "myHandler" && "Hello" to me in 2 seconds


... I think.
Fred.
Last edited by FredBeck on Sat Jan 23, 2016 11:18 am, edited 1 time in total.

FredBeck
Posts: 77
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: Execute function after some time

Post by FredBeck » Sat Jan 23, 2016 2:21 am

This works here

Code: Select all

on mouseUp
   put  "get " & quote & "myFunction(" & "Hello" &")" & quote into tFunc
    send tFunc to me in 2 seconds
end mouseUp
   
function myFunction theText
   answer theText
end myFunction

Code: Select all

on mouseUp
   put  "myHandler" && quote & "Hello" & quote into tCom
    send tCom to me in 2 seconds
end mouseUp
   
command myHandler theText
   answer theText
end myHandler

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Execute function after some time

Post by Simon » Sat Jan 23, 2016 2:28 am

Thank you Fred,
It was bugging me that I left it so Hacky.
A little change

Code: Select all

on mouseUp
   put "Hello" into tWord
   put  "get " & quote & "myFunction(" & tWord &")" & quote into tFunc
   send tFunc to me in 2 seconds
end mouseUp
   
function myFunction theText
   answer theText
end myFunction
As theText should normally be changing

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

FredBeck
Posts: 77
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: Execute function after some time

Post by FredBeck » Sat Jan 23, 2016 11:13 am

Does it make sense to even do that, since you can pass parameters to a handler?
I mean, a function usually takes a parameter in and spits a result out, and a handler is usually for doing stuff involving other objects (such as the answer dialog window).
Plus the fact that sending a function needs so many more quotes and brackets... I always struggle with those.
F.

FredBeck
Posts: 77
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: Execute function after some time

Post by FredBeck » Sat Jan 23, 2016 12:37 pm

Simon,
I don't think your code is hacky at all, the more I think about it.
The quotes positions are really awkward :

get "myFunction(Hello)"
normally puts "myFunction(Hello)" into the variable it

I would expect
get myFunction("Hello")
to be the thing to send...

ps,
I just checked, It works in Variable Checking / Strict Compilation Mode too. I didn't think it would...
Last edited by FredBeck on Sat Jan 23, 2016 1:52 pm, edited 1 time in total.

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm
Location: Croatia

Re: Execute function after some time

Post by croivo » Sat Jan 23, 2016 1:04 pm

Guys thank you very much :)

FredBeck
Posts: 77
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: Execute function after some time

Post by FredBeck » Wed Jan 27, 2016 11:49 pm

Better syntax I think :

Code: Select all

on mouseUp
   put "hello" into tWord
   put  "value(myFunction(" & quote & tWord & quote & "))"  into tFunc
   send tFunc to me in 2 seconds
end mouseUp
       
function myFunction theText
   answer theText
end myFunction
F.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”