It is a function used to run a process regularly for a specified period of time.
package main import "time" func SetInterval(someFunc func(), ms int, async bool) chan bool { duration := time.Duration(ms) * time.Millisecond stopper := make(chan bool) ticker := time.NewTicker(duration) go func(){ for { select { case <-stopper: ticker.Stop() return case _ = <-ticker.C: if async { go someFunc() } else { someFunc() } } } }() return stopper }
package main import "fmt" func main() { counter := 0 interval := SetInterval(func() { counter++ fmt.Printf("Şafak sayar: %d\n", counter) }, 500, false) for { if counter == 5 { interval <- true fmt.Println("Program sonlandı!") break } } }