返回函数时的SetTimeout?

时间:2017-01-06 19:57:57

标签: javascript reactjs

我有条件地根据国家传递风格。我希望在渲染组件之后应用转换,否则转换不会发生。我被告知实现这一目标的最佳方法是使用setTimeout,但是如何才能超时返回函数?

style= {
    transition: 'transform 1s',
    ...this.zoomedIn && this.mobileZoom(i),
}

zoomedIn () {
  return ['zoomIn', 'addBg', 'zoom', 'removeBg'].includes(this.state.zoom)
}

mobileZoom () {
  const transform = this.state.transform.mobile
  if (window.innerWidth < 600) {
    return {
      zIndex: 1070,
      transform: 'translateY(' + transform.offset + 'px) scale(' + transform.scale + ')'
    }
  }
}

1 个答案:

答案 0 :(得分:0)

如果将其返回值设置为setTimeout函数,则无法从函数返回值,但您可以执行此操作:

zoomedIn () {
var send = new promise(function(resolve, reject) {
     setTimeout(function() {
       resolve(['zoomIn', 'addBg', 'zoom', 'removeBg'].includes(this.state.zoom));
     });
   });
   return send;
}

//call function like this

zoomedIn().then(function(value) {
  console.log(value); 
});