定时器

作者: 冷洪林 | 来源:发表于2016-12-23 10:50 被阅读276次

setTimeout和clearTimeout基本用法

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Navigator,
  Image,
  TouchableHighlight,
  ScrollView,
  Animated
} from 'react-native';

import MyScene from './MyScene';

export default class DTest extends Component {

  constructor(props) {
      super(props);  
      this.state={
      }
  }
  componentDidMount() {
    this.timer = setTimeout(
      () => {
        this.setState({content:'我是定时器打印的内容...One'})
      },
      5000
    );
    this.timer_two = setTimeout(
      () => {
        this.setState({msg:'我是定时器打印的内容...Two'})
      },
      1000
    );
  }
  componentWillUnmount() {
    this.timer && clearTimeout(this.timer);
    this.timer_two && clearTimeout(this.timer_two);
  }
  render() {
    return (
      <View style={{margin:20}}>
        <Text style={styles.welcome}>
           定时器实例
        </Text>
        <Text>{this.state.content}</Text>
        <Text>{this.state.msg}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
});

// 注册应用(registerComponent)后才能正确渲染
// 注意:只把应用作为一个整体注册一次,而不是每个组件/模块都注册
AppRegistry.registerComponent('DTest', () => DTest);

setInterval和clearInterval基本用法

index.ios.js:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Navigator,
  Image,
  TouchableHighlight,
  ScrollView,
  Animated
} from 'react-native';

import MyScene from './MyScene';

export default class DTest extends Component {

  constructor(props) {
      super(props);  
      this.state={
        sum:0,
      }
  }

  render() {
    return (
      <View style={styles.welcome}>
        <MyScene text='测试setInterval'
        onPress={()=>{
            this.interval=setInterval(() => {this.setState({sum:(this.state.sum+1)});
          },400);
        }}/> 
        <Text>{this.state.sum}</Text>
        <MyScene text='测试setInterval'
        onPress={()=>{
          this.interval && clearInterval(this.interval)
        }}/>
      </View>    
    );
  }
}


const styles = StyleSheet.create({
  welcome: {
    flex:1,
    alignItems:"center"
  },
});
// 注册应用(registerComponent)后才能正确渲染
// 注意:只把应用作为一个整体注册一次,而不是每个组件/模块都注册
AppRegistry.registerComponent('DTest', () => DTest);

MyScene.js:

import React, { Component, PropTypes } from 'react';
import { View, Text, TouchableHighlight } from 'react-native';

export default class MyScene extends Component {
 
  render() {
    return (
      <View style={{flex:1, justifyContent:"center", alignItems:"center"}}>
        <TouchableHighlight onPress={this.props.onPress}>
          <Text>{ this.props.text }</Text>
        </TouchableHighlight>   
      </View>
    )
  }
}

效果图:


相关文章

  • 2017.12.21学习总结

    下午学习了定时器,定时器分为高级定时器、通用定时器和基本定时器,我们主要研究通用定时器。 定时器中断实现步骤:...

  • javascript笔记6

    定时器-间歇性定时器 定时器-延时定时器 认识DOM 间歇性定时器var time = window.setInt...

  • 定时器弹框、定时器基本用法、定时器动画、时钟

    定时器弹框: 定时器基本用法: 定时器动画: 时钟:

  • 无标题文章

    iOS NSTimer使用详解-开启、关闭、移除 定时器定时器详解ios定时器关闭定时器NSTimer 1、要使用...

  • 定时器

    1.倒计定时器(setTimeout) clearTimeout清除定时器2.循环定时器(setInterval)...

  • 第十三节 JavaScript 定时器 单线程

    一、定时器 1. JS存在两种定时器 setTimeout() 延迟定时器 setInterval() ...

  • 定时器 - OC

    定时器的定义 创建一个定时器并启动这个定时器 停止定时器 后续了解:NSTimer invalidate不起作用h...

  • 定时器 类型转换 封闭函数

    定时器定时器在javascript中的作用1、制作动画2、异步操作3、函数缓冲与节流 定时器类型及语法 /*定时器...

  • STM32--------定时器

    STM32F103一共有11个定时器,其中: 2个高级定时器 4个普通定时器 2个基本定时器 2个看门狗定时器 1...

  • 定时器

    定时器弹窗 定时器基本用法 定时器动画 时钟 倒计时 变量的作用域

网友评论

      本文标题:定时器

      本文链接:https://www.haomeiwen.com/subject/fvckvttx.html