setTimeout()无法返回值

时间:2016-06-11 09:19:16

标签: javascript

我已添加 func displayAlert(title: String, message: String) { // Display alert message to user. Passing in title and message which will be displayed. if #available(iOS 8.0, *) { let a = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) a.addAction((UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in self.dismissViewControllerAnimated(true, completion: nil) }))) self.presentViewController(a, animated: true, completion: nil) } else { // Fallback on earlier versions } } ,但它无效。我希望在声音结束5秒后显示问题。

setTimeout

1 个答案:

答案 0 :(得分:0)

异步中的

setTimeout()并返回回调函数中的值。改变你的js的结构。

而不是:

self.getQuestionText = function () {

  startSound('levelq', false);
    setTimeout(function () {
      return self.questions[self.level() - 1].question;
    }, 5000);

  }

// The rest of the code

使用回调结构:

self.getQuestionText = function () {

    startSound('levelq', false);
    setTimeout(function () {
        self.getQuestionText = self.questions[self.level() - 1].question;

        // The rest of the code
    }, 5000);

}