jQuery setInterval not looping - javascript

I have a function that I want to loop. I found the easiest way to do this was using setInterval. When I tried this the function only runs once. Please advise where I am going wrong. Cheers
function empMove() { $('.emp-wrap').css('margin-top', '-100px')};
setInterval(empMove, 2000);
I have a div with multiple rows and I want to show only one at a time, hence I am decreasing the margin-top each time.

The current code sets the top margin to -100px. Try
function empMove() { $('.emp-wrap').css('margin-top', '-=100')};

I highly recommend the jQuery timing plugin (2KB) (GitHub Repository, Docs).
It provides easy-to-use loop animations and much more. Have a look:
function empMove() {
$('.emp-wrap').css('margin-top', '-=100px').repeat().wait(2000);
};

function displayOnly() {
var initHeight = $('.wrapper').height();
$('.wrapper').height(initHeight);
$('p').not('.read').fadeOut(2000, function () {
if ($(this).is(':last-child')) {
$('.wrapper').animate({
height: $('.inner').height()
}, 2000);
}
});
}
$("button").click(displayOnly);
In action on fiddle

Related

Jquery/JS malfunction when window is not visible

I'm using JS to make a simple function that displays 3 items one at a time. Works well when you're looking at the page, but when you minimize or change tabs then return, all three items are shown.
Anyone know why? It's as if fadeIn(x) keeps running but hide() stops working. I even checked with different classes.
Here is the code:
$(document).ready(function () {
function start() {
$(".featured-items").hide();
$( ".item-1" ).fadeIn('slow');
setTimeout(one, 5000);
}
function one() {
$(".featured-items").hide();
$( ".item-2" ).fadeIn('slow');
setTimeout(two, 5000);
}
function two() {
$(".featured-items").hide();
$( ".item-0" ).fadeIn('slow');
setTimeout(start, 5000);
}
setTimeout(start, 5000);
});
Problem solved, check the best answer below and make sure to read comments to get a good understanding. Thanks to all
(Updated to provide complete answer)
Your original code is too complex, and a more flexible and simpler implementation is to have just one function, and an array of items in the gallery. Secondly, you should modify your code so the fadeIn animation starts immediately instead of getting queued. Having only one function instead of several makes alterations such as this easier.
Note that in the code below, as in your original code, the various gallery items are classes rather than single element ids and could fade in multiple items.
var gallery = [ '.item-1', '.item-2', '.item-3' ];
var i = 0;
function galleryEvent() {
$(".featured-items").hide();
$( gallery[i] ).fadeIn({duration: 'slow', queue: false});
i = (i + 1) % gallery.length;
setTimeout(galleryEvent, 5000);
}
// start everything off....
galleryEvent();

How to call a Javascript Function and make it perform twice properly?

Good evening, I have a javascript function to set background color, etc when user onclick the table row. The function does not perform correctly unless it is called twice or when I press F12 for development tools, similar situation as Function doesn't correctly perform unless it is called twice.
I managed to handle it as below but the problem is that, when the user onclick the table row, it needs around 2-3 seconds for the background color to be change. How can I reduce the time for the function to perform twice?
JavaScript
<SCRIPT LANGUAGE="JavaScript">
setBackGroundColorOnIE (tableRowNumber) {
//........
//........
setBackGroundColorOnIE (tableRowNumber)//I need the function to perform twice in one call
}
</SCRIPT>
I know it is a bad practice, but I'm really have no idea on how to fixed the compability issues in IE9. So, I came out with something like this. Need some hints and advices, thanks in advanced.
you could add some condition to check if your function need to run again, like:
function setBackGroundColorOnIE (tableRowNumber) {
//check if your condition is met
if( some_condition) {
var timerId = setTimeout(function() {
setBackGroundColorOnIE (tableRowNumber);
}, 5000); //set to 5 seconds
}
else {
clearTimeout ( timerId );
}
}
//call the function
setBackGroundColorOnIE(some_value);
An easy solution would be to include jquery ui which would allow you to change the color and then have a callback when the color change is completed. Here is a JS fiddle with jquery and jquery ui. http://jsfiddle.net/kqMs9/
$(function(){
$('button').on('click', function(){
$('.background').animate({
backgroundColor: '#000'
}, 1500, function(){ alert('background-color changed!');});
});
});

How do I create a pause or delay when hovering over javascript list items?

hoping someone can help.
I'm a javascript novice. I have a list of names that, when hovered over, display a box with that person's contact information.
The problem I'm having is that the box displays too fast; causing boxes to fire off rapidly when mousing over multiple names.
Link: http://law.nd.edu/faculty/
Here's what I believe is the relevant code:
<script>
jQuery(".directory-list li").hover(
function() {
jQuery(this).find(".directory-info").fadeIn(200); ;
},
function() {
jQuery(this).find(".directory-info").fadeOut(50);;
}
);
</script>
Thanks for any help.
Use hoverIntent instead.
There is a nice little plugin for it, that is the easiest way to do it.
http://cherne.net/brian/resources/jquery.hoverIntent.html
It will keep your elements from rapid-firing.
The easiest way would be to add a delay before your fadeIn:
jQuery(this).find(".directory-info").delay(300).fadeIn(200);
You can introduce a delay by using setTimeout as follows:
var hoverTimer;
jQuery(".directory-list li").hover(function() {
var elem = jQuery(this).find(".directory-info");
hoverTimer = setTimeout(function() {
elem.fadeIn(200);
}, 1000); // wait for one second and then fadeIn
},
function() {
clearTimeout(hoverTimer);
jQuery(this).find(".directory-info").fadeOut(50);
});
Check out this fiddle, think this is what you want. The other answer that uses timeoutes will loose the context of this inside the setTimeout() function and will not work.
http://jsfiddle.net/RZUVS/1/.

jQuery toggle() function. I would like some critique or suggestions for better practice

My code is working as I would like it to, but I am looking for any critique on how it could be trimmed or modified to be more efficient:
$('.toggle-comments').hide(); // this hides the results div when the page loads.
$(".comments-toggle-click").click(function () {
$(".toggle-comments").slideToggle("slow");
$(".comments-toggle-click").remove();
});
Instead of calling $('.toggle-comments').hide(); on page load why not render the comments with display:none;?
CSS:
.toggle-comments {
display: none;
}
JavaScript:
$('.comments-toggle-click').click(function () {
$('.toggle-comments').slideToggle('slow', function () {
$('.comments-toggle-click').remove()
})
})
The trick is to pass .slideToggle a callback function if you want to delay the removal of the link until the animation is complete. This may or may not be the problem you elude to in your question.

jQuery automate function call

How do I call a given function every X seconds?
In this case, I made a function that scrolls some images. I want the image to change based on a given interval of time, for example, every 5 seconds, but I really have no idea.
No need for jQuery here, plain JavaScript using setInterval() will do:
function myFunctionName() {
//change image here
}
setInterval(myFunctionName, 5000);
Or the anonymous version:
setInterval(function () {
//change image here
}, 5000);
Try
setInterval(function, Xseconds);
If you want jQuery to do it for you, there is a plugin, jquery.cycle.all. This will make creating the transition very easy. If you're using jQuery already, then it might be a good fit. Otherwise, the setInterval is easy to work with that other posters already mentioned.

Categories

Resources