DEV Community

wei chang
wei chang

Posted on

A Detailed Explanation of the Timer in the HarmonyOS Cangjie development Language

Today, it's time for the popular science session that everyone enjoys again. It can also be said to be the pitfall session. Hahaha. Today, let's talk about the timer in Cangjie's language development. This part is really interesting.

Why do I say so? Because you can hardly find any documentation about Cangjie's timer, nor are there any related code prompts. It can be said that it just keeps writing without a word.

However, it was still found by You LAN Jun with some clues. Today, I'll share it with everyone.

Cangjie's Timer is hidden in the std.sync package and is called Timer. However, when using it, importing only the Timer package is not enough. We need to import these modules:

import std.time.*
import std.sync.Timer
import std.sync.CatchupStyle
Enter fullscreen mode Exit fullscreen mode

The writing method of the timer is also unique. It has several modes. Let's introduce them one by one to you below

Timer.after(Duration.minute, { => 
    AppLog.info('这是after计时器'); 
    return Duration.second
    }) 
Enter fullscreen mode Exit fullscreen mode

The meaning of this line of code is that the timer is executed after one minute, and then App.info printing is executed once every minute. This mode is rather strange. Its execution interval is returned in the execution method.

Another rather odd point, I wonder if you have noticed, is that the two time parameters, the delay time and the execution interval, in the timer use enumerations of the Duration type. That is to say, only a few values set by it can be used. Currently, the given time values include Zero, second, minute, hour, etc. It can be seen that they are all time integers. If you want to execute once every two seconds or once every half a second, it is probably not possible.

Image description

Timer.once(Duration.minute,{=> 
  AppLog.info('这是once计时器');
  })
Enter fullscreen mode Exit fullscreen mode

This mode should be recognizable at a glance by everyone. It is a timer that is executed only once, and the delay time is one minute later.

Timer.repeat(Duration.Zero, Duration.second, {=> 
  AppLog.info('这是repeat计时器'); 
  }, style: CatchupStyle.Skip) 
Enter fullscreen mode Exit fullscreen mode

"repeat" is a timer for repetitive execution, "last.zero" is the time for delayed execution, and "last.second" is the interval between executions. Here, a new parameter, "style", has emerged. Its name is the leveling strategy. The official explanation given is: When the execution time of a Task is too long, the execution time points of subsequent tasks may be delayed. Different leveling strategies are applicable to different scenarios.

Timer.repeatDuring(period: Duration, delay: Duration, interval: Duration, task: () -> Unit, style: CatchupStyle)  
Enter fullscreen mode Exit fullscreen mode

This timer is similar to the "repeat" above. It has an additional parameter "period", which means the maximum duration of the repetition period. Similarly, there is "repeatTimes", which specifies the maximum number of executions of the task:

Timer.repeatTimes(count: Int64, delay: Duration, interval: Duration, task: () -> Unit, style: CatchupStyle)
Enter fullscreen mode Exit fullscreen mode

The above is the detailed content about the timer in Cangjie's language development. Thank you for reading. Once again, I wish everyone a pleasant holiday. #HarmonyOS Language ## Cangjie ## Shopping #

Top comments (0)