2016-07-11 10 views
0

Ich versuche, einen einfachen Countdown in Native React zu machen. Ich habe den TimerMixin benutzt, aber nichts wird aktualisiert.Native Timer reagiert nicht triggern

Ich bin mir nicht sicher, warum der Timer das Timeout nicht ausführt.

import React from 'react-native' 
 
var TimerMixin = require('react-timer-mixin'); 
 
var reactMixin = require('react-mixin'); 
 

 
const { 
 
    Dimensions, 
 
    StyleSheet, 
 
    ScrollView, 
 
    View, 
 
    Image, 
 
    Text, 
 
    Component, 
 
} = React; 
 

 
class Countdown extends Component { 
 
    constructor() { 
 
     super(); 
 
     this.state = { 
 
     targetDate: "09/09/2016", 
 
     time: { 
 
      total: 0, 
 
      days: 0, 
 
      hours: 0, 
 
      minutes: 0, 
 
      seconds: 0 
 
     } 
 
     } 
 
     
 
    } 
 
    
 
    componentDidMount() { 
 
     this.timer = TimerMixin.setTimeout(() => { 
 
      console.log('Refreshing ' + this.state.time.seconds); 
 
      this.refresh(); 
 
     },100); 
 
    } 
 
    
 
    componentWillUnmount() { 
 
     TimerMixin.clearTimeout(this.timer); 
 
    } 
 

 
    refresh() { 
 
     var t = Date.parse(this.state.targetDate) - Date.parse(new Date()); 
 
     var seconds = Math.floor((t/1000) % 60); 
 
     var minutes = Math.floor((t/1000/60) % 60); 
 
     var hours = Math.floor((t/(1000*60*60)) % 24); 
 
     var days = Math.floor(t/(1000*60*60*24)); 
 
     var time = { 
 
     'total': t, 
 
     'days': days, 
 
     'hours': hours, 
 
     'minutes': minutes, 
 
     'seconds': seconds 
 
     }; 
 
     
 
     this.setState({targetDate: this.state.targetDate, time: time});  
 
    } 
 
    
 
    render() { 
 
     // Render omitted for simplicity of question 
 
    } 
 
} 
 

 
reactMixin(Countdown.prototype, TimerMixin); 
 

 
module.exports = Countdown;

erhalte ich nur "Refreshing 0" ist, einmal bei der Initialisierung.

Antwort

0

Gelöst. Das Problem war, dass ich setTimeout anstelle von setInterval gesetzt hatte. Offensichtlich würde dies dazu führen, dass der Timer nur einmal nach 100 ms und nur dann einmal läuft.