Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

Call a function every X seconds when activated - Scala

I have a variable which is going to help me check if 10 seconds has passed.

private val currTime: Long = System.currentTimeMillis()
private val time: Long = 10000

I want something where, if a function foo is called, it will reset currTime to be the current time. That's the easy part which I can do. The tough part, and part 2 of this, is that if currTime at any point exceeds time, I want to change a boolean (say isHappy) from true to false - this can be in a function called bar.

I come from C#, so i'm not sure how to do this - typically we'd use a dispatcher timer but i have no idea how to achieve this is Scala.

I've seen scheduler examples and fixed interval examples, but this is more of a moving target... a condition that can be met at any time.

In simpler terms, it's like having 2 ints. Int A and Int B. When the sum of A and B is over 10, a function is triggered. A and B are forever changing in different functions in that class. How can we create a trigger?

question from:https://stackoverflow.com/questions/65926570/call-a-function-every-x-seconds-when-activated-scala

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...