How to find out if setTimeout is running on the background - javascript

I am having a problem to stop the setTimeout function fired previously.
I found some relative questions and the giving solution is to put the time_out variable outside of the function to make it work. I tried but still not working.
this.A = function(){
if(time_out) clearTimeout(time_out);
// time_out is nothing here and this will return error
time_out = setTimeout(function(){ }, 3000);
}
-------------------------------------------------
this.time_out = 0;
this.A = function(){
if(this.time_out) clearTimeout(this.time_out);
//will run through, but the setTimeout setup previously will keep running...
this.time_out = setTimeout(function(){ }, 3000);
}
Update
I tired put the whole function into a element and call it out when reset
if(el.data('time_out')) clearTimeout(el.data('time_out'));
This works perfect

make sure you are in the proper scope.
var timerFun = function(){
var me = this;
me.timer= undefined;
me.startTimeout = function(){
me.timer = setTimeout(me.SetterFunc, 3000);
};
me.setterFunc = function(){
alert('oh hai!');
me.ClearTimer();
};
me.clearTimer = function(){
if(me.timer!= undefined){
me.clearTimeout(me.timer);
}
};
return me;
};

Create trigger functions for timeout
startTimeOut(){
started = 1;
setTimeout(yourFunction, 2000);
}
well then u can check if started=1 u can stop it and if started=0 means it is stopped
stopTimeOut(){
started = 0;
clearTimeout(yourFunction);
}

It's quite easy to stop a setTimeout.
var timeout = setTimeout(function(){
myFunc();
}, 1000);
function myFunc(){
//do something...
};
To clear the timeout
clearTimeout(timeout);

Related

Calling a function onclick start and onclick stop

I am using the below functions to start and stop spin.Basically I am trying to add an Autospin button and tried below approach but its not working.Start function is working but stop not working.
var nIntervId;
this._onAutoSpin = function(){
s_oGame.onSpin();
nIntervId = setInterval(this._onAutoSpin, 10 * 1000);
};
this._offAutoSpin = function(){
clearInterval(nIntervId);
};
The issue is because you're recursively starting a new interval every time it fires, therefore you only stop the latest timer, not the previous.
To fix this change your logic so that there is no possible recursion and there is only ever one interval running:
function Foo() {
var nIntervId;
this._onAutoSpin = function() {
nIntervId = setInterval(this._doAutoSpin, 1 * 1000); // modified for demo
}
this._doAutoSpin = function() {
console.log('spinning...');
};
this._offAutoSpin = function() {
console.log('stopped');
clearInterval(nIntervId);
};
}
var foo = new Foo();
foo._onAutoSpin();
setTimeout(foo._offAutoSpin, 5000); // stop after 5 seconds
If you want to do this recursively then you need to use setTimeout(). You also need to cache the this reference so that it is maintained within the successive calls:
function Foo() {
var nIntervId;
this._onAutoSpin = function() {
var _this = this;
console.log('spinning...');
nIntervId = setTimeout(function() {
_this._onAutoSpin();
}, 1 * 1000); // modified for demo
}
this._offAutoSpin = function() {
console.log('stopped');
clearInterval(nIntervId);
};
}
var foo = new Foo();
foo._onAutoSpin();
setTimeout(foo._offAutoSpin, 5000); // stop after 5 seconds
try to use setTimeout instead of setInterval because setInterval creates a new timer every time without destroying the last one.
var nIntervId;
this._onAutoSpin = function(){
s_oGame.onSpin();
nIntervId = setTimeout(this._onAutoSpin, 10 * 1000);
};
this._offAutoSpin = function(){
clearTimeout(nIntervId);
};

Delete global variables nodejs

So I have a node app that when it's started makes an object.
function my_object(){
}
my_object.prototype.say_lol = function() {
setTimeout(function(){
console.log('lol');
}, 1000);
};
var ping = new my_object();
ping.say_lol();
process.on( 'SIGINT', function() {
delete global.ping; // Please?
// pseudo code to go update something in the database or whatever
setTimeout(function(){
process.exit();
}, 4000);
});
When it ends I need to delete that object to prevent it from firing lol over and over while the timeout is pending. Is there any elegant way of doing this?
You need to capture the id of what setTimeOut returns and then clear it if you don't want to fire at a later point.
my_object.prototype.say_lol = function() {
return (setTimeout(function(){
console.log('lol');
}, 1000));
};
var ping = new my_object();
var timeOutId = ping.say_lol();
process.on( 'SIGINT', function() {
clearTimeout(timeOutId);
process.exit();
});
If the event is not fired yet, it will be cancelled. If its already fired, it fails silenty.
I don't know of a way to stop the callback from firing. My approach would be to keep track of ping's state in another variable and use an if statement wrapping whatever chunk of code fires the unwanted callbacks.
function my_object(){ };
var stillHere = true;
my_object.prototype.say_lol = function() {
setTimeout(function(){
if(stillHere){
console.log('lol');
}
}, 2000);
};
var ping = new my_object();
ping.say_lol();
process.on( 'SIGINT', function() {
delete global.ping; // Please?
stillHere = false;
setTimeout(function(){
process.exit();
}, 4000);
});
Although I understand it's not a true answer, but this solved my issue. I ran the node process in a cluster and killed off any children that I wanted to delete. This worked in my instance as the children were performing endless tasks and when I needed them to be stopped that was the only logical way I could abort them.

clearTimeout Function not working properly on dialog when open

Here i attached my code. am clear the time out function when dialog is open.but its not working properly.When i open my dialog the count down decreasing from 20 but in between 20 seconds if i close and open the dialog , the timing is collapsing each other.
function(){
var time =20;
flag = false;
clearTimeout(startTimer);
startTimer = function(){
if(!flag){
var finTime = time - 1;
time = finTime;
setTimeout(startTimer,1000);
if(time==0){
flag = true; }
$("#input").text(time);
} else {
clearTimeout(startTimer,1000);
}
};
setTimeout(startTimer,1000);
};
I have tried this code also
dialogOpen = function(){
$("#dialog").dialog('open');
startTimer();
stopTimer();
}
startTimer = function() {
time = 20;
flag = false;
setTimeout(startTime, 1000);
};
stopTimer = function() {
flag = true;
time = 0;
clearTimeout(startTime);
};
startTime = function(){
if(!flag){
var finTime = time - 1;
time = finTime;
setTimeout(startTime,1000);
if(time==0){
flag = true;
}
$("#input").text(time);
} else {
clearTimeout(Time);
}
};
You have to assign setTimeout to a variable
var timer;
timer = setTimeout(startTimer,1000);
and you can clear the setTimeout using clearTimeout by passing the variable.
clearTimeout(timer);
To call clearTimeout() you want to pass in the intervalVariable that was returned by setTimeout(), not the function, so it would look like:
var intervalVariable = setTimeout(startTimer,1000);
clearTimeout(intervalVariable);
It's best to thing of intervalVariable as the ID for the timeout that is then used by the browser to cancel it.
Set a global variable before your startTimer function, then set it to the return of setTimeout() and use it in place of startTimer in clearTimeout().

Add a timeout to "setInterval()"?

I have a setInterval function like below on a Divbox so if I leave a divbox, this setInterval is triggered:
setInterval("playthis()", 1000);
What I want it to do: If I leave the divbox and lets say within the next 2 second rehover it, the setInterval should not triggered.
Is this possible?
You can use cousins setTimeout and clearTimeout to set a function callback that invokes your setInterval only after 2 uninterrupted seconds:
var handle = null;
function yourDivboxLeaveHandler() {
handle = setTimeout(startPlay, 2000);
}
function yourDivboxHoverHandler() {
if (handle !== null) {
clearTimeout(handle);
handle = null;
}
}
function startPlay() {
setInterval(playthis, 1000); // use function references please, not eval
handle = null;
}
You will want much better variable/function names than this though.
Yes. Just make some creative use of clearInterval().
In other words, no, such a feature doesn't come out-of-the-box, but you can build it yourself by calling clearInterval() if the mouse re-enters the divbox before the interval is triggered.
For example:
var divBox = document.getElementById('MyDivBox');
var TimeoutHandle = null;
divBox.onmouseover = function()
{
if ( TimeoutHandle != null )
{
clearTimeout(TimeoutHandle);
}
}
divBox.onmouseout = function()
{
TimeoutHandle = setTimeout(function()
{
TimeoutHandle = null;
setInterval(playthis, 1000);
}, 2000);
}
First of all is a bad practice to have the code evalued in a setInterval so you should avid double quotes. Then you can clear the interval like this:
var int = setInterval(playthis, 1000);
clearInterval(int)

Javascript, AJAX, Extend PHP Session Timeout, Bank Timeout

Greetings,
I have the following JS code:
var reloadTimer = function (options) {
var seconds = options.seconds || 0,
logoutURL = options.logoutURL,
message = options.message;
this.start = function () {
setTimeout(function (){
if ( confirm(message) ) {
// RESET TIMER HERE
$.get("renewSession.php");
} else {
window.location.href = logoutURL;
}
}, seconds * 1000);
}
return this;
};
And I would like to have the timer reset where I have the comment for RESET TIMER HERE. I have tried a few different things to no avail. Also the code calling this block is the following:
var timer = reloadTimer({ seconds:20, logoutURL: 'logout.php',
message:'Do you want to stay logged in?'});
timer.start();
The code may look familiar as I found it on SO :-)
Thanks!
First of all, you need to use the new operator in var timer = new reloadTimer, and also reloadTimer should be capitalized into ReloadTimer to signify that it needs to be used with new.
The reason why you need new is because the function references this and when used without new this will be the global scope instead of the instance it self.
To reset a timer you just call window.clearTimeout with the timers reference as the parameter
var timer = window.setTimeout(....
...
window.clearTimeout(timer);
UPDATE
By RESET do you actally mean to restart the timer?
If so, just use setInterval instead of setTimeout
UPDATE 2
And here is a slightly better approach (if you still want to use such a class to encapsulate something so trivial)
var ReloadTimer = function(options){
var seconds = options.seconds || 0, logoutURL = options.logoutURL, message = options.message;
var timer;
return {
start: function(){
timer = setInterval(function(){
if (confirm(message)) {
$.get("renewSession.php");
}
else {
clearInterval(timer);
window.location.href = logoutURL;
}
}, seconds * 1000);
}
};
};
var myTimer = new ReloadTimer({
seconds: 20,
logoutURL: 'logout.php',
message: 'Do you want to stay logged in?'
});
myTimer.start();
You could execute the function again with the same parameters?

Categories

Resources