When my AJAX call is completed I need to call setInterval, but when two AJAX calls are made it also calls setInterval twice. How can I stop the previous setInterval?
$(document).ajaxComplete(function () {
$(".iframeFake").load(function () {
var islem = setInterval(function () {
$('.iframeFake').each(function (event) {
console.log(1);
}, 1000);
});
});
In chrome console in first post i get 1 per second - but after second post i get double 1 per second. Where is my problem?
var islem;
$(document).ajaxComplete(function () {
$(".iframeFake").load(function () {
clearInterval(islem);
islem = setInterval(function () {
$('.iframeFake').each(function (event) {
console.log(1);
}, 1000);
});
});
If you want to maintain that there is always one interval, store the variable at a higher scope, and cancel before you create to stop any lingering intervals.
DEMO of the principle in action
Related
I'm unsure on why this isn't working:
$(document).ready(function() {
setInterval(RefreshDiv, 2000);
})
function RefreshDiv(){
$('#box').load('messages.php #box', function() {
$('#box').on('load', function() {
$('#box').scroll(0, 50);
});
});
}
The tags are correct and the .load() part works every two seconds but I don't understand why my complete event to scroll down 50px isn't working?
I've also tried another method to scroll:
var log = document.querySelector('#box');
log.scrollTop = log.scrollHeight - log.clientHeight;
but this also doesn't execute on load
Edit #1
jQuery($ => {
setInterval(RefreshDiv, 2000);
})
function RefreshDiv() {
$('#box').load('messages.php #box', () => {
$('#box').scrollTop(50);
});
}
The load event only fires on certain elements such as img and the window object. As such I presume #box is not one of them.
You don't actually need the load event handler anyway as the callback itself runs when the load() method completes its request. Try this:
jQuery($ => {
setInterval(RefreshDiv, 2000);
})
function RefreshDiv() {
$('#box').load('messages.php #box', () => {
$('#box').scrollTop(5000);
});
}
It's also worth noting that sending AJAX requests every 2 seconds is not ideal, as it will not scale as you have more concurrent visitors to your site, and can lead to server performance problems. There's likely to be a much better alternative, depending on what it is you're doing.
I'm having issues getting clearInterval to work when I try to bind it to a button click. Also, apparently the function is starting on it's own... Here's my code
var funky = setInterval(function() {
alert('hello world');
}, 2000);
$('#start').click(function() {
funky();
});
$('#stop').click(function() {
clearInterval(funky);
});
Here's a js fiddle
You have forgot to add jquery library and have made wrong assignment, it needs to be inside callback function.
Working example:
var funky;
$('#start').click(function() {
funky = setInterval(function() {
alert('hello world');
}, 2000);
});
$('#stop').click(function() {
clearInterval(funky);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="start">start</button>
<button id="stop">stop</button>
First off, yes, when you assign a variable to a function, it self invokes.
Secondly, your click events are not working because you need assign the interval to the variable on the click, not invoke the function - there is no function to invoke, as you would see if you looked at your developer console.
Lastly, it is good practice to wrap the jQuery code in the document ready function to ensure all of your event handlers get bound properly.
$(function () {
var funky;
$('#start').click(function() {
funky = setInterval(function() {
alert('hello world');
}, 1000);
});
$('#stop').click(function() {
clearInterval(funky);
});
});
You're saving the wrong value. Try this:
var funky = function() {
alert('hello world');
}
var funkyId = setInterval(funky, 2000);
$('#start').click(function() {
funky();
});
$('#stop').click(function() {
clearInterval(funkyId);
});
Here I am giving you the idea.
declare a variable e.g. let x;
create a function which you want to bind with setInterval.
e.g.
function funky() {
alert("Hello World");
}
assign start.onclick to a function which will assign the setInterval to x.
e.g start.onclick = function(){
clearInterval(x); // to prevent multiple interval if you click more than one
x = setInterval(funky, 2000); // assign the setInterval to x
};
assign stop.onclick to clearInterval(x) to stop the interval.
e.g. stop.onclick = function() {
clearInterval(x); // to stop the interval
};
That's it. Easy right.
Currently i have the following
$(document).ready(function () {
$('#abc').click(function () {
setTimeout(function () {
//do stuff
}, 2000);
});
});
Is there a better way to do it, perhaps something like this
$('#abc').click(function () {
// sleep/delay or whatever
//do stuff
});
No, there is no better way. In order to sleep synchronously you need to use a spinlock which will use all the browser's resources and spike the CPU for 2 seconds (the duration of the "sleep").
Stick with the asynchronous version.
$('#abc').click(function () {
$(this).delay(seconds);
//do stuff
});
Try this
I dunno wheather it works
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Below is a jQuery statement which hides a div element on it's click event. I'd like the element to fade out regardless if it isn't clicked after 5 seconds. Is there a simple way I can call the fadeOut function in the same expression or without the click event interfering with the animation?
$(".fadeOutbox").click(function(){
$(this).fadeOut('slow');
});
Most jQuery components are chain-able, your function as it stands returns a reference to the initial object.
You can achieve what you want simply by using:
$(".fadeOutbox").click(function () {
$(this).stop().fadeOut('slow');
}).delay(5000).fadeOut('slow');
Basically reads as onclick, fade out otherwise fade out after 5 seconds.
I assume this is inside another function that shows the box to begin with. This solution will hide the box after 5 seconds, or immediately upon clicking.
var $box = $('.fadeOutbox');
var fadeOut = function() {
clearTimeout(timeout);
$box.fadeOut('slow');
};
var timeout = setTimeout(fadeOut, 5000);
$box.click(fadeOut);
Save the fact that the user has clicked or not and test it in the timer
var isClicked = false;
setTimeout(function () {
if(!isClicked)
$(".fadeOutbox").fadeOut('slow');
}, 5000);
$(".fadeOutbox").click(function () {
isClicked = true;
});
Try this:
var wasClicked = false;
$(".fadeOutbox").click(function () { wasClicked = true; });
setTimeout(function () {
if(wasClicked = false)
$(".fadeOutbox").fadeOut('slow');
}, 5000);
Use a timeout not inside of the click handler:
setTimeout(function () {
$(".fadeOutbox").fadeOut('slow');
}, 5000);
Your jQuery code becomes:
// set a timeout for 5 seconds
setTimeout(function () {
$(".fadeOutbox").fadeOut('slow');
}, 5000);
// attach click handler
$(".fadeOutbox").on("click", function () {
$(this).fadeOut('slow');
});
JSFIDDLE
Edit to clarify:
var clear = setTimeout(function(){ $(".fadeOutbox").fadeOut('slow'); }, 5000);
$(".fadeOutbox").on('click', function(){
clearTimeout(clear);
});
Demo: http://jsfiddle.net/mGbHq/
Try holding a variable for the timeout and clear it every time the user clicks.
Working example
// Timeout variable
var t;
$('.fadeOutBox').click(function()
{
$box = $(this);
$box.fadeIn("fast");
// Reset the timeout
clearTimeout(t);
t = setTimeout(function()
{
$box.fadeOut("slow");
}, 5000);
});
Hope this helps you.
Wow, none of the answers gives the simple solution: Use setTimeout and cancel the timeout on click:
$(".fadeOutbox").click(function () {
// Cache the jQuery object
var $this = $(this);
// Do we already have a timer running?
var timer = $this.data("timer");
if (timer) {
// Yes, cancel it
clearTimeout(timer);
$this.removeData("timer");
}
// (You may want an `else` here, it's not clear)
// In five seconds, fade out
$this.data("timer", setTimeout(function() {
$this.removeData("timer");
$this.fadeOut('slow');
}, 5000));
});
I'm not 100% sure that the above is triggering on the events you want, but the two pieces of relevant code are this, which schedules the timed action:
// In five seconds, fade out
$this.data("timer", setTimeout(function() {
$this.removeData("timer");
$this.fadeOut('slow');
}, 5000));
and this, which cancels it (for instance, on click):
var timer = $this.data("timer");
if (timer) {
// Yes, cancel it
clearTimeout(timer);
$this.removeData("timer");
}
Try
$('#div').delay(5000).fadeOut(400)
Demo
My application reloads data every 500ms. How do I have to change the code to not reload every 500ms but to wait for 500ms after the last reload to trigger a new one?
App = Ember.Application.create({
ready: function() {
var switchboard = App.Switchboard.find(switchboard_id);
setInterval(function() {
switchboard.reload();
}, 500);
}
});
I have just done something similar. You should use activate property on your route (http://emberjs.com/api/classes/Ember.Route.html#method_activate).
Checkout this pull request: https://github.com/chrmod/rowmark/pull/2/files
Some example:
App.NoteRoute = Ember.Route.extend({
activate: function() {
this.interval = setInterval(function() {
this.get('controller').set('toSave', true);
}.bind(this), 5000);
}
})
UPDATE
I understand you wrong. Sorry for that.
First of all you need to know that find from Ember Model or Ember Data returns promises (http://emberjs.com/blog/2013/05/28/ember-data-0-13.html)
I think you can do such trick to implement that:
App = Ember.Application.create({
ready: function() {
var switchboard;
setInterval(function() {
switchboard = App.Switchboard.find(switchboard_id).then(function(){
setTimeout(function(){}, 499);
});
}, 1);
}
});
First of all we run setInterval to run this in infinity loop. Next in each loop iteration we find Switchboard and when Ember data loads from external server those data that run function that is passed to then. This function simply wait 499ms :)