Incrementing a setInterval() value from a global - javascript

I'm trying to increment the timeout on either setInterval, or setTimout. However, my global is not working. I'm not to familiar with javascript. Please help!
//What I'm trying to do:
$(document).ready(function() {
var joeIncrement=1;
setInterval(function() {
joeIncrement++;
$('#comment_counter').load('get_commentcount.php');
}, 15000*joeIncrement);
});

$(document).ready(function() {
var joeIncrement=1;
var interval = function(){
setTimeout(function() {
joeIncrement++;
$('#comment_counter').load('get_commentcount.php');
interval();
}, 15000*joeIncrement);
}
interval(joeIncrement)
});

Related

Removing class after X seconds?

I'm trying this below code:
$(document).ready(function() {
$("button").hover(
function() {
$(this).addClass("active");
},
function() {
$(this).removeClass("active");
}
);
});
Try this: http://plnkr.co/edit/Xlco44QPWvKEh1jb0gDf?p=preview
var button = $('button');
button.hover(
function() {
button.addClass('active');
setTimeout(function() {
button.removeClass('active');
}, 3000);
},
function() {
button.removeClass('active');
}
);
From what you said in your previous comment below, you tried setTimeout and it didn't work because of the the way you used this. The value of this inside the timeout function wasn't the same as in your outer function, so jQuery didn't match your button element.
Better to define the button once as a variable, and reuse the variable, that use repeated jQuery selectors.
UPDATE: Here's a slightly more sophisticated version that keeps the setTimeout timers from piling up:
$(function() {
var button = $('button');
var timeout = 0;
button.hover(
function() {
button.addClass('active');
timeout = setTimeout(function() {
button.removeClass('active');
timeout = 0;
}, 2000);
},
function() {
button.removeClass('active');
if (timeout) {
clearTimeout(timeout);
timeout = 0;
}
}
);
});
I have created a jQuery extention just for that! This is extremely common in web development:
jQuery.addTempClass.js
$.fn.addTempClass = function(tempClass, duration){
if( !tempClass )
return this;
return this.each(function(){
var $elm = $(this),
timer;
$elm.addClass(tempClass);
// clear any timeout, if was any
clearTimeout( $elm.data('timeout') )
timer = setTimeout(function(){
$elm.removeClass(tempClass).removeData('timeout');
}, duration || 100);
// set the timer on the element
$elm.data('timeout', timer);
});
};
Usage example:
$(document.body).addTempClass('some_class', 2000)
You can include this script as a dependency file for your build system or simply copy-paste this piece of code somewhere in your code, just after jQuery has loaded, so it will be available everywhere afterwards.

clear timeout after trigger click

If class triggered run another function only once and clear Timeout. I am using Firefox.
Expecting
var i = setInterval(function() {
if $('[style*="display: inline;"]').click();
function start timeout() {
document.querySelector('.body_inside_inside_once_').click()
clearTimeout();
}, 200);
}, 1000);
Your questions is not clear ,and contains syntax error but for rough idea...The following code might help you to clearTimeout upon clicking a HTML element.
var i = setInterval(function() {
function start timeout() {
// Operation
}}, 1000);
document.querySelector('.body_inside_inside_once_').click(function(){
clearInterval(i);
});

jQuery and setTimeout: TypeError: g.nodeName is undefined

I'm having problems with a script. I'm using a jQuery.post function inside a setTimout and it returns TypeError: g.nodeName is undefined on Firebug. Here's my script:
jQuery(function($) {
var timer;
$("#tabela-orcamento .item .item-qtd .qtd-item").keyup(function() {
clearTimeout(timer);
timer = setTimeout(function() {
$.post("../../aj_orc.php?op=atualizaQtd", {
item: $(this).parents(".item").attr("data-item"),
qtd: $(this).val()
}, function(data) {
$("#retornos").html(data);
});
},1000);
});
});
Is something wrong?
Your running into a problem, because the this inside your timeout refers to another context as you might think. Just introduce another that as a intermediate variable:
jQuery(function($) {
var timer;
$("#tabela-orcamento .item .item-qtd .qtd-item").keyup(function() {
clearTimeout(timer);
var $that = $(this);
timer = setTimeout(function() {
$.post("../../aj_orc.php?op=atualizaQtd", {
item: $that.parents(".item").attr("data-item"),
qtd: $that.val()
}, function(data) {
$("#retornos").html(data);
});
},1000);
});
});

Repeat code every x seconds but not if [insert check here]

This is a followup to this question, where I found out how to make code be repeated every x seconds. Is it possible to make an event that can change this? I.e. I have a checkbox which is meant to control whether this is repeated or not, so I figured I'd need something like this:
$(checkbox).bind("change", function() {
switch(whether if it is ticked or not) {
case [ticked]:
// Make the code repeat, while preserving the ability to stop it repeating
case [unticked]:
// Make the code stop repeating, while preserving the ability to start again
}
});
I have no idea what I could put in the cases.
You can do it by assigning your setInterval function to a variable.
var interval = setInterval(function() { }, 1000);
and then you can stop setInterval by
clearInterval(interval);
p.s.
to start your interval you need to call var interval = setInterval(function() { }, 1000); again
You can either stop and start the interval:
var timer;
function start() {
timer = window.setInterval(function(){
// do something
}, 1000);
}
function stop() {
window.clearInterval(timer);
}
start();
$(checkbox).bind("change", function() {
if ($(this).is(':checked')) {
start();
} else {
stop();
}
});
Or you can have a flag causing the interval to skip the code:
var enabled = true;
var timer = window.setInterval(function(){
if (!enabled) {
// do something
}
}, 1000);
$(checkbox).bind("change", function() {
enabled = $(this).is(':checked');
});
function fooFunc() {
$('#foo').text(+new Date());
}
var id;
var shouldBeStopped = false;
$('input').change(function() {
if (shouldBeStopped)
clearInterval(id);
else
id = setInterval(fooFunc, 200);
shouldBeStopped = !shouldBeStopped;
});​
Live DEMO

JavaScript/jQuery clearInterval being set in .each

So I have an interval I create for each of my posts, the issue is that I load new posts and remove the old ones, so obviously I'd like to stop the interval for the previous posts. However I can't seem to figure out how to do this. Could someone explain to me how to properly go about doing this? I'm completely lost.
$(".post").each(function(){
myInterval = setInterval("postStats('"+$(this).attr('id')+"')", 500);
});
function postStats(pid) {
//do some stuff
}
$(".button").click(function(){
clearInterval(myInterval);
});
You can store the interval ID in a data attribute:
$(".post").each(function () {
var that = this;
var myInterval = setInterval(function () {
postStats(that.id);
}, 500);
$(this).data("i", myInterval);
});
and clear the interval specific to each .post like so:
$(".button").click(function () {
// assuming the button is inside a post
clearInterval($(this).closest(".post").data("i"));
});
and like SiGanteng said, you should pass a function object to setInterval rather than a string, which only gets eval'd.
You need to keep one handle for each interval that you start:
var myIntervals = [];
$(".post").each(function(){
var id = $(this).attr('id');
var handle = window.setInterval(function(){
postStats(id);
}, 500);
myIntervals.push(handle);
});
function postStats(pid) {
//do some stuff
}
$(".button").click(function(){
$.each(myIntervals, function(i, val){
window.clearInterval(val);
});
myIntervals = [];
});

Categories