when I call timer.cancel (), the clock stops working. But when calling back to start a new session, I discover that the _u variable in the old session still counts from itself and parallel with new one.
Please help !
startTime(60);
timer.cancel();# It worked.
timer = null;
startTime(60);# but when recall. It working with 2 session. (old and new)
//Call timer.cancel() _u variable stop at 40.
//Recall startTime() _u variable counts with 2 session (40) and (60)
startTime(reset){
int _u = reset;
const oneSec = const Duration(seconds: 1);
timer = Timer.periodic(
oneSec,
(Timer timer) => setState(
() {
if (_u < 1) {
timer.cancel();
// call my handle
} else {
_u = _u -1;
if(_u <0){
timer?.cancel();
}
}
},
),
);
Cancel the timer if it is not null. The code below was tested
void startTime(reset) {
int _u = reset;
const oneSec = const Duration(seconds: 1);
if (timer != null) {
timer.cancel();
}
timer = Timer.periodic(oneSec, (Timer t) {
if (_u < 1) {
t.cancel();
// call my handle
} else {
_u = _u - 1;
print(_u);
if (_u < 0) {
t?.cancel();
}
}
});
}
Related
I am using timer.periodic to call some functions at different times. The problem which I am facing is that the timer is much slower than real life like for example what you will see in my code that the timer should finish in 5 seconds but in real life its taking 25 seconds to finish.
void startTheTimer(){
var counter = 5;
final zeroDurationTimer = Timer.run(() {
_StartDataCollection();
});
Timer.periodic(const Duration(seconds: 5), (timer) {
print(timer.tick);
counter--;
if (counter == 2) {
_StopDataCollection();
}else if (counter == 1){
createUser();
}
if (counter == 0) {
print('Cancel timer');
timer.cancel();
print(numbers.length);
print(fifo.length);
}
});
}
the print on the compiler shows the timer ticks as 1-2-3-4-5 but its taking it too long to print 2 and then same goes for the rest of the ticks.
Anyone knows what is going on?
Timer.periodic(const Duration(seconds: 5), (timer) { //do function } mean, it spent 5s to do this function
So, if you do this function 5 times, it will spent 25s
Change to 1s will work:
Timer.periodic(const Duration(seconds: 1), (timer) {
print(timer.tick);
counter--;
if (counter == 2) {
_StopDataCollection();
}else if (counter == 1){
createUser();
}
if (counter == 0) {
print('Cancel timer');
timer.cancel();
print(numbers.length);
print(fifo.length);
}
});
or use for-loop instead timer
for(int i = 0; i < 5; i++){
await Future.delayed(Duration(seconds: 1));
counter--;
print(counter);
if (counter == 2) {
_StopDataCollection();
}else if (counter == 1){
createUser();
}
if (counter == 0) {
print('Cancel timer');
print(numbers.length);
print(fifo.length);
}
}
i try to reconnect with the server , the connection established very well and it work every 5 sec very well also , but when i put a condiotn to control the conection it not working and still get new conection added , new connection added
how can i implement this by dart
var nbmer = 0;
var channel;
Timer? timer;
connect() {
Socket.connect(IP, PORT).then((Socket sock) async {
channel = sock;
sock.write('hello world'
);
});
}
try {
if (nbmer == 0) {
timer = Timer.periodic(Duration(seconds: 5), (Timer t) => connect());
}
} catch (e) {
print(e.toString());
}
nbmer += 1;
You should put your condition in the connect() function, because Timer.periodic will be periodically executed and not check your condition (if (nbmer == 0)).
Solution
Your code should look something like this:
var nbmer = 0;
var channel;
Timer? timer;
connect() {
if (nbmer == 0) {
nbmer += 1;
Socket.connect(IP, PORT).then((Socket sock) async {
channel = sock;
sock.write('hello world');
});
}
}
try {
timer = Timer.periodic(Duration(seconds: 5), (Timer t) => connect());
} catch(e) {
print(e.toString());
}
I am trying to make snake 2 in flutter. And I have used Timer.periodic() for game loop. And I tried specifying duration as 1 seconds. But the code inside the Timer.periodic() runs multiple times in a second. I also tried debugging (though I am terrible at that) and found that the code inside the Timer.periodic() ran multiple times without stepping out of it. Though while debugging this couild happen as the code pauses for input. But I'm not sure about anything .Here is my code -
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
class SnakePage extends StatefulWidget {
#override
_SnakePageState createState() => _SnakePageState();
}
class _SnakePageState extends State<SnakePage> {
int score = 0;
String swipe = '';
bool running = false;
int iterates = 0;
List snake = [
[
[4, 3],
1,
true
],
[
[4, 2],
1,
false
],
[
[4, 1],
1,
false
],
];
// Convert radians to degree
double radians(double degree) {
return ((degree * 180) / pi);
}
void turn(moveEvent) {
double angle = radians(moveEvent.delta.direction);
if (angle >= -45 && angle <= 45) {
this.swipe = 'Swipe Right';
} else if (angle >= 45 && angle <= 135) {
this.swipe = 'Swipe Down';
} else if (angle <= -45 && angle >= -135) {
this.swipe = 'Swipe Up';
} else {
this.swipe = 'Swipe Left';
}
}
int toIndex(coOrdinates) {
return ((coOrdinates[0] + 1) * 10) + coOrdinates[1];
}
void run() {
this.running = true;
Timer.periodic(
Duration(
milliseconds: 500,
), (timer) {
this.setState(() {
this.iterates += 1;
this.swipe = this.iterates.toString();
for (var i = 0; i < this.snake.length; i++) {
this.snake[i][0][1] += 1;
if (this.snake[i][0][1] == 10) {
this.snake[i][0][1] = 0;
}
}
});
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('WC'),
),
body: Listener(
onPointerMove: this.running
? (moveEvent) => this.turn(moveEvent)
: (moveEvent) => this.run(),// Where the function is being called
child: Container();
);
}
}
And please pardon me for code being a mess and not well commented.
Any Help would be appreciated!
The problem is that, every time you execute the run() method, a new timer is created and you listen for it, again. The old timer is not stopped, so it keeps firing.
The solution is, before you create a timer, cancel the previous one. Something like this:
class _SnakePageState extends State<SnakePage> {
Timer? _myTimer;
void run() {
this.running = true;
_myTimer?.cancel(); //in case we have a timer, we'll cancel it.
_myTimer = Timer.periodic(. // assing new timer to our variable.
Duration(
milliseconds: 500,
), (timer) {
this.setState(() {
this.iterates += 1;
this.swipe = this.iterates.toString();
for (var i = 0; i < this.snake.length; i++) {
this.snake[i][0][1] += 1;
if (this.snake[i][0][1] == 10) {
this.snake[i][0][1] = 0;
}
}
});
});
}
}
I am trying to achieve a direct task, I want to autoplay the next track after the current track finishes, but sometimes the onComplete event is triggered before even the track is completed which lead to skip one of the tracks
the package I am using is: audioplayers ^0.14.2
the tracks are fetched from the database they are not local tracks
void play(List<SoundTrack> tracks){
audioPlayer.play(tracks[currentIndex].url);
setIsTalking = true;
audioPlayer.onPlayerCompletion.listen((event) {
if(currentIndex < tracks.length-1) {
next(tracks);
} else {
audioPlayer.release();
setIsTalking = false;
setPlayerState = PlayerState.paused;
}
print('completed this track, current index is' + currentIndex.toString());
});
}
void next(List<SoundTrack> tracks){
setCurrentIndex = currentIndex +1;
play(tracks);
}```
Try something like this:
define
int currentIndex = 0;
and
play(List<String> urlList, int currentIndex) async {
int result = await audioPlayer.play(urlList[currentIndex]);
if (result == 1) {
print('Success: is playing');
} else {
print('Error on audio play');
}
audioPlayer.onPlayerCompletion.listen((event) {
if(currentIndex < urlList.length-1){
currentIndex = currentIndex + 1;
nextTrack(urlList, currentIndex);
print("NEXT AUDIO! $currentIndex");
} else {
print("AUDIO COMPLETED PLAYING");
}
});
}
void nextTrack(List<String> urlList, int currentIndex) {
play(urlList, currentIndex);
}
In your play botton you call
nextTrack(urlList, currentIndex)
I tested and for me it is working.
I create a setInterval method like below, I want to stop it anytime from anywhere. By the way, that function is in a if-else statement, so I cannot access the stop function.
How can it be possible?
var intervalID = setInterval(function () {
if (flag) {
stopTimer();
Pages.Page1.Label1.text = "";
}
if (time == 0) {
stopTimer();
Pages.Page1.Label1.text = "";
Device.makeCall(callNumber.rows[0][0]);
} else
updateTime();
}, 1000);
function updateTime() {
time = time - 1;
Pages.Page1.Label1.text = time;
}
function stopTimer() {
clearInterval(intervalID);
}
Just set the flag variable to true.
flag = true;
Alternatively, i would suggest to declare your functions outside of the if / else scope .
For instance, create a timer instance with start / stop methods as such :
timer = function() {};
timer.prototype.start = function ()
{
this.timerId = setInterval(function () {
/* ... */
/* call to doWork here or simply your code */
}, 1000);
}
timer.prototype.doWork = function ()
{
// override here if you want and wall doWork in setInterval
}
timer.prototype.stop = function ()
{
clearInterval(this.timerId);
}
var t = new timer();
// start the timer
t.start();
// stop the timer
t.stop();
This way you can handle your timer as you want.