HarmonyOS 如何延迟一秒后执行某项操作?

如题:HarmonyOS 如何延迟一秒后执行某项操作?

阅读 482
1 个回答

可以添加一个定时器,到时间后就会执行需要的操作,示例如下:

@Entry
@Component
export struct BookingBallFieldItemView {
  @State item: string = 'xxxx'
  @State detailIsShow: Visibility = Visibility.None
  fontStyleAttr1: TextStyle = new TextStyle({ fontColor: '#ffae18ff' });
  mutableStyledString1: MutableStyledString = new MutableStyledString(this.item, [{
    start: 2,
    length: 1,
    styledKey: StyledStringKey.FONT,
    styledValue: this.fontStyleAttr1
  }]);
  controller2: TextController = new TextController();

  async onPageShow() {
    this.controller2.setStyledString(this.mutableStyledString1)
  }

  build() {
    Column() {
      Row() {
        Column() {
          Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Start }) {
            Text(undefined, { controller: this.controller2 })
              .fontColor('#333333')
              .fontSize(16)
              .maxLines(1)
              .height(20)
              .textAlign(TextAlign.Start)
              .textOverflow({ overflow: TextOverflow.Ellipsis })
              .key('mutableStyledString1')
              .onClick(() => {
                setTimeout(() => {
                  this.detailIsShow = Visibility.Visible
                }, 3000)


              })
            Text('玄幻小说').visibility(this.detailIsShow).margin({ left: 30 })
          }.width("100%")
        }
        .layoutWeight(1)
        .margin({ left: 15, top: 12 })
        .alignItems(HorizontalAlign.Start)
        .justifyContent(FlexAlign.Start)
      }.width('100%').justifyContent(FlexAlign.Start)
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏