Gil
Gil

Reputation: 103

Swift 2 - Timed Actions one second apart?

I'm trying to get output like so:

1
(then a one second delay)
Hello
2
(then a one second delay)
Hello
3
(then a one second delay)
Hello

But instead I get
1
2
3
(then a one second delay)
Hello
Hello
Hello

Here's my for loop invoking the NSTimer

    var timer = NSTimer()

    for i in 1...3 {
        print("\(i)");
        timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(MainVPScreenViewController.printTest), userInfo: nil, repeats: false)
    }

And here's the selector method:

func printTest() {
    print("Hello")
}

Thanks in advance for any help you can provide

Upvotes: 0

Views: 55

Answers (4)

Ivan Gutnik
Ivan Gutnik

Reputation: 86

Try this solution without NSTimer:

var i = 1

func printHello() {

    print(i)

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
        print("Hello")
        i +=1
        if i <= 3 {
            printHello() 
        }
    }

}

override func viewDidLoad() {
    super.viewDidLoad()

    printHello()
}

Upvotes: 1

Feldur
Feldur

Reputation: 1169

This does it with repeat false, and is set up to be in a playground:

import XCPlayground

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

@objc class Foo: NSObject {
    static var timer = NSTimer()
    var i:Int

    override init() {
        Foo.timer = NSTimer()
        i = 1
    }

    func schedule() {
        print("\n\(i)");
        i += 1
        Foo.timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
                                                       target: self,
                                                       selector: #selector(printTest),
                                                       userInfo: nil,
                                                       repeats: false)
    }

    @objc func printTest() {
        print("Hello")
        if i < 5 {
            schedule()
        }
    }
}
let bar = Foo()
bar.schedule()

Upvotes: 0

xmhafiz
xmhafiz

Reputation: 3538

Use timer with repeat to true. So in your view controller would be like this

var timer = NSTimer()
var counter = 0
var max = 10
let delay = 1 // in second

override func viewDidLoad() {
    super.viewDidLoad()
    timer = NSTimer.scheduledTimerWithTimeInterval(delay, target: self,
                                                selector: #selector(self.printTest), userInfo: nil, repeats: true)
}

func printTest() {
    counter += 1
    print(counter)
    print(hello)

    if counter == maxNumber {
        timer.invalidate()
    }
}

Upvotes: 0

Reinier Melian
Reinier Melian

Reputation: 20804

I need 2 NSTimers to do this, this is my approach

    class ViewController: UIViewController {

    var i = 1

    override func viewDidLoad() {
        super.viewDidLoad()

        beginPrinting()
    }

    func beginPrinting() {
         var timer2 = NSTimer()
        if(i <= 100)
        {
             timer2 = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(self.printWithDelay), userInfo: nil, repeats: false)
        }
    }

    func printWithDelay()
    {
        var timer = NSTimer()
        print("\(i)");
        i += 1
        timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(self.printTest), userInfo: nil, repeats: false)
    }

    func printTest() {
        print("Hello")
        beginPrinting()
    }

}

Hope this helps you

Upvotes: 0

Related Questions