小编dar*_*ger的帖子

在Rails中创建倒计时器,在后端重置

我目前正在创建一个带有计时器的Rails测验应用程序,该计时器会倒计时回答问题的秒数.

在我的第一个版本中,我这样做的方法很简单:为计时器创建一个变量,为秒创建一个实例,然后将其作为测验的Javascript的一部分,如下所示:

Quiz = function() {
  this.quizCurrent = 0;
  this.score = 0;
  this.seconds = 10;
  this.timing = this.seconds;
  this.container = $('#trivia');
  this.participationId = null;
 }

 Quiz.prototype.init = function() {
  $('#trivia').on('click', '.btn.btn-primary-questions', this.checkAnswer.bind(this));
  $('div.timer strong').text(this.seconds)

  this.start();
 }
Run Code Online (Sandbox Code Playgroud)

我现在想使用Rails将其移动到后端.之前的问题引用了倒计时宝石,但这似乎提出了两个问题:

1)我需要重复调​​用日期/时间并验证每个问题(我不认为我可以用倒计时做),以及;

2)我理想情况下可以将此值作为变量传递给Javascript.

在该gem的文档中,它显示了如何直接将倒计时时间传递到视图中.但是,我正在使用倒计时值(换句话说,当它达到零时)强制用户在录制错误答案时发出下一个问题,如下所示,作为验证的一部分:

Quiz.prototype.timer = function() {
 var that = this;

 this.timing--;
   $('div.timer strong').text(this.timing);
   if (this.timing <= 0) {
     self.stop(); 
  $.getJSON('/api/skip_question/' + this.participationId, function() {
    that.nextQuestion();
   })
  }}
Run Code Online (Sandbox Code Playgroud)

关于我如何"检查"后端的时间,同时仍然进行测验检查以确保时间没有用完的任何想法?

javascript ruby-on-rails

11
推荐指数
1
解决办法
1308
查看次数

在Swift中从documentDirectory中删除所有文件

我正在制作音频应用,用户可以下载本地存储到documentDirectoryusing中的文件FileManager

接下来,我想允许用户使用一个按钮删除所有文件。在文档中,有一种删除项目方法

这是我的代码:

@IBAction func deleteDirectoryButton(_ sender: Any) {

    let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

        do {
            try FileManager.default.removeItem(at: documentsUrl, includingPropertiesForKeys: nil, options: [])

        } catch let error {
            print(error)
        }
    }
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不会出错Ambiguous reference to member 'removeItem(atPath:)'

有没有更好的方法来一次访问documentDirectory和从目录中删除所有文件?

ios nsdocumentdirectory swift

4
推荐指数
2
解决办法
3353
查看次数

使用 AVAudioPlayer 进行 UISlider 更新

我正在制作一个简单的音频播放器应用程序,它允许用户使用 UISlider 更改他们在曲目中的位置。

它不做的事情:滑块不会根据曲目的进度进行更新。我在这里发现了一个类似的问题,第二个答案是响应AVAudioPlayer- 建议使用currentTime更新滑块。我想尝试这样做。

这是我的代码:

import UIKit
import AVFoundation

class MusicViewController: UIViewController {

    ....

    @IBOutlet var Slider: UISlider!

    @IBAction func changeAudioTime(_ sender: Any) {
        Slider.maximumValue = Float(audioPlayer.duration)
        audioPlayer.stop()
        audioPlayer.currentTime = TimeInterval(Slider.value)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }

    func updateSlider(){

        Slider.value = Float(audioPlayer.currentTime)
        print("Changing works")
    }
Run Code Online (Sandbox Code Playgroud)

第一个函数工作正常——用户可以改变他们所在的位置。但是,我的第二个功能应该Slider.value根据currentTimeAudioPlayer 的 进行更改。它不这样做。

它也没有打印,所以我相信它没有触发。

我是 Swift 的新手,所以我可能会遗漏一些东西。有任何想法吗?

avaudioplayer ios swift swift3

3
推荐指数
1
解决办法
3414
查看次数

使用AVAudioPlayer在Swift中播放远程mp3文件

我是Swift的新手,但我想更改视图控制器以在我的iOS应用中播放远程mp3文件。我从以下代码开始,在本地播放歌曲,并且可以正常运行(此后具有播放器的功能):

import AVFoundation

class Music1ViewController: UIViewController {

    //5 -
    var songPlayer = AVAudioPlayer()
    //15 -
    var hasBeenPaused = false

    //6 -
    func prepareSongAndSession() {

        do {
            //7 - Insert the song from our Bundle into our AVAudioPlayer
            songPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "localsong", ofType: "mp3")!))
            //8 - Prepare the song to be played
            songPlayer.prepareToPlay()
Run Code Online (Sandbox Code Playgroud)

在查看了AVAudioPlayer 文档之后.prepareToPlay()预加载了缓冲区,这使我认为我所需要做的就是更改初始化程序以定位URL。

然后更改初始化器:

songPlayer = try AVAudioPlayer(contentsOf: URL(string: "https://s3.amazonaws.com/kargopolov/kukushka.mp3")!)
Run Code Online (Sandbox Code Playgroud)

我没有在XCode中得到任何错误,但是当我运行它时,我在控制台中看到一个错误,Thread 1: EXC_BAD_ACCESS (code=1, address=0x48)这让我觉得我正在接近这个错误。

有没有更好的方法来访问远程mp3文件?

ios swift

2
推荐指数
2
解决办法
2659
查看次数

PagesController中几乎相同的方法在Rails 4中产生未定义的方法错误

我的Rails 4应用程序中有三个表 - 一个用于游戏,类别主题.这两个类别主题有一列:name,而游戏包括像信息starts_at游戏开始时.

在我的PagesController中,我可以使用params值显示来自GameTopic的数据find_by:

 topic = Topic.find_by_name(params[:topic])
 @games = Game.for_topic(topic).upcoming.order(:starts_at)
Run Code Online (Sandbox Code Playgroud)

这很好用.

奇怪的是,当我使用相同的推理但使用Category而不是Topic时,就像这样:

 category = Category.find_by_name(params[:category])
 @games = Game.for_category(category).upcoming.order(:starts_at)
Run Code Online (Sandbox Code Playgroud)

我收到一条错误消息:

undefined method `for_category'
Run Code Online (Sandbox Code Playgroud)

这让我感到困惑,因为我肯定是在我的for_表达式中定义类别和使用它.我在思考中犯了错误吗?

额外

CreateCategories迁移

 class CreateCategories < ActiveRecord::Migration
  def change
    create_table :categories do |t|
    t.belongs_to :topic, index: true
     t.string :name, :null => false

     t.timestamps
    end
   end
  end
Run Code Online (Sandbox Code Playgroud)

CreateTopics Migration

 class …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

0
推荐指数
1
解决办法
44
查看次数

使Rails循环遍历元素以表示表中的项目

我正在构建一个Rails应用程序,我使用六边形网格显示其博客帖子的链接作为每个帖子的图像六边形.为清楚起见,这是静态帖子的样子:

 <ul id="hexGrid">
            <li class="hex">
                <a class="hexIn" href="contact">
                    <img src="https://structurepoint.files.wordpress.com/2014/09/wp_20140804_041.jpg" alt="" />
                    <h1>This is a title</h1>
                    <p>Some sample text about the article this hexagon leads to</p>
                </a>
            </li>
            <li class="hex">
                <a class="hexIn" href="#">
                    <img src="https://structurepoint.files.wordpress.com/2014/09/wp_20140804_041.jpg" alt="" />
                    <h1>This is a title</h1>
                    <p>Some sample text about the article this hexagon leads to</p>
                </a>
            </li>
            <li class="hex">
                <a class="hexIn" href="#">
                    <img src="https://structurepoint.files.wordpress.com/2014/09/wp_20140804_041.jpg" alt="" />
                    <h1>This is a title</h1>
                    <p>Some sample text about the article this hexagon leads to</p>
                </a>
            </li>
        </ul>
Run Code Online (Sandbox Code Playgroud)

要查看它的完整代码,这里 …

jquery ruby-on-rails

0
推荐指数
1
解决办法
25
查看次数

在 Swift 3 中删除 UserDefaults 的单个键值

我正在我的 iOS 应用程序中跟踪用户的首选项UserDefaults——当用户选择一个表格单元格时,它会将单元格的文本添加到我的键中。那部分工作。

现在,我想允许用户在选择一行时从键中删除匹配的键值。如果一个单元格显示“第 1 集”,它应该从UserDefaults.

文档中,有一个实例方法removeObject。这是我写的:

    let defaults = UserDefaults.standard
    var myarray = defaults.stringArray(forKey: "SavedStringArray") ?? [String]()
    if let datastring = TableData[indexPath.row] as? String {
        if !myarray.contains(datastring) {
            myarray.append(datastring)
            defaults.removeObject(myarray, forKey: "SavedStringArray")
            defaults.synchronize()
        }
Run Code Online (Sandbox Code Playgroud)

这将返回一个错误Extra argument in call- 我认为这意味着myarray,但如果我不能添加该参数,我怎么能告诉它只删除一个值(存储在 中myarray)?

如果我打印UserDefaults.standard,它将返回存储的剧集值列表,如`[“第一集”、“第二集”、“第三集”]

知道Third Episode在此处单击该单元格时如何删除吗?

ios swift userdefaults

0
推荐指数
1
解决办法
2956
查看次数