Getting jquery to delay after adding a class - javascript

I want a script to halt before continuing after adding a class, below are two attempts that seem to fail.
window.setTimeout($("#"+item).addClass("highlight"), 5000 );
$("#"+item).addClass("highlight").delay(5000);
Where am I going wrong here?

Another way:
$("#"+item).addClass("highlight");
setTimeout(function(){
//rest of the code
}, 5000);

You want something like:
function addClassAndDelay(item, nextMenu) {
$("#"+item).addClass("highlight");
window.setTimeout(function() { restOfCode(nextMenu); }, 5000);
}
function restOfCode(nextMenu) {
jQT.goTo('#'+nextMenu, 'slide');
}

May be you are looking for the jQuery Highlight plugin?
$("#mydiv").click(function () {
$(this).effect("highlight", {}, 3000);
});

Related

JS setTimeout stack

why does setTimeout not work? And how to do this action properly? I need to get 30s delay every submit. Sorry for newbie question, but i am newbie.
if (event.target.id.indexOf('submit') === 0)
{ post1000.submit(); setTimeout('post1001.submit();', 30000); }
{ post1001.submit(); setTimeout('post1002.submit();', 60000); }
...
{ post5092.submit(); setTimeout('post5093.submit();', 122790000); }
}, false);
You can also try something like this;
setTimeout(yourSubmitFunction, 3000)
function yourSubmitFunction() {
//do whatever you want to do you can define submit here
}
You can call setTimeout in a loop, like for each element in your array which has your "post****" variables.
I believe you shouln't use a string as first parameter for setTimeout();
Here is this function definition :
setTimeout(function,milliseconds,param1,param2,...)
Try with this code sample, or update yours accordingly :
setTimeout(function(){ alert("Hello"); }, 3000);

Function not repeating in Javascript?

I have a function in Javascript that I need to repeat every 5 seconds.
Here is the simple code:
function myFunk() {
$('body').addClass('polyonloaded');
setTimeout(myFunk, 5000);
}
myFunk();
It runs the function once, and does not repeat it anymore. What went wrong with my code?
Your code is working; try printing something:
function myFunk() {
$('body').addClass('polyonloaded');
console.log('hello');
setTimeout(myFunk, 5000);
}
myFunk();
The problem: since the added class is always the same, nothing happens:
the class is already added; it won't be added again.
If you want to add, remove, add, remove, ... etc. use toggleClass:
function myFunk() {
$('body').toggleClass('polyonloaded');
setTimeout(myFunk, $('body').hasClass('polyonloaded') ? 4000 : 5000);
}
myFunk();
You have to add and remove the class.
Set an interval that adds the class, then set a timeout inside to wait a bit that removes it, just before the next interval.
Forked yours here:
http://codepen.io/snlacks/pen/KwoELp
$(document).ready(function() {
setInterval( function() {
$('body').addClass('polyonloaded');
setTimeout( function(){
$('body').removeClass('polyonloaded');
}, 4000)}, 5000 );
});
Seems to be working fine:
function myFunk() {
console.log('running');
setTimeout(myFunk, 1000);
}
myFunk();
http://jsfiddle.net/dem6u89L/

SetInterval using linkObj

With this code I expect to see a 'yo' added to the console every second while I'm hovering over .cell-top. But I get one 'yo' and that's it.
function cellUp(linkObj) {
console.log('yo');
}
$(".cell-top").hover(function() {
setInterval(cellUp($(this)), 1000);
});
Any idea what I can do to get my expected results?
PS. I'm using linkObj to get $(this) in a function within cellDown, I didn't include the function because that's unrelated to the issue I'm having. I did include the linkObj because I figured it may be part of the issue.
Since you're using jQuery, you can use $.proxy.
$(".cell-top").hover(function() {
setInterval($.proxy(cellUp, null, $(this)), 1000);
});
var interval;
function cellUp(linkObj) {
console.log(linkObj);
}
$(".cell-top").hover(function() {
var self = this;
interval = setInterval(function(){cellUp($(self))}, 1000);
},function() {
clearInterval(interval);
});

hide a message after 30 seconds.?

How can after click on button show a message and next hide it after 30 seconds?
Like:
$('#message').live('click', function() {
$('#sm').hide();
$('#sm').hide().show('slow').html('You have successfully registered');
// how is hide "$('#sm')" after 30 seconds??
});
Please give me example in http://jsfiddle.net/
$('#message').live('click', function() {
$('#sm').hide().show('slow').html('You have successfully registered');
setTimeout(function(){ $('#sm').hide(); }, 30000);
});
JSFiddle Example
setTimeout(function() {
$('#sm').hide();
}, 30000);
in your third line you write:
$('#sm').hide().show('slow').html('You have successfully registered').delay(30000).hide();
hope it works
You are looking for setTimeout it takes a function and milliseconds as parameter. In your case it would be something like:
setTimeout(function() { $('#sm').hide() ; }, 30000);
Use either javascript's native setTimeout function or jQuery's delay function. If you choose the latter all you have to do is add:
.delay(30000).hide();
at the end of your existing code like so:
$('#sm').hide().show('slow').html('You have successfully registered').delay(30000).fadeOut();

Refactor $(document).ready(), currently have 2 instances

I've got following JavaScript functions but want to refactor the $(document).ready() as I've got 2 instance of it. How can I achieve this?
FlashMessenger = {
init: function() {
setTimeout(function() {
$(".flash").fadeOut("slow", function () {
$(".flash").remove();
});
}, 5000);
}
}
SelectLanguage = {
init: function() {
$('#selectLanguageId').change(function() {
$('#frmSelectLanguage').submit();
});
}
}
$(document).ready(FlashMessenger.init);
$(document).ready(SelectLanguage.init);
It’s perfectly acceptable to set multiple handlers for $(document).ready, although you may have a good reason to do otherwise that I’m not aware of. You might be interested in knowing that $(handler) can be used as shorthand for $(document).ready(handler):
$(FlashMessenger.init);
$(SelectLanguage.init);
If you really want them in one call though, try this:
$(function() {
FlashMessenger.init();
SelectLanguage.init();
});
First off, there's no reason you have to combine them.
But if you want to:
$(document).ready(function(jq){
FlashMessenger.init(jq);
SelectLanguage.init(jq);
});
Breaking it down:
Create a function to do all your init (it can be named or anonymous; the one above is anonymous).
Have it call the other init functions, passing in the jQuery instance that jQuery passes you just in case they use it.
You might choose to wrap each init call in a try/catch block as well, so that errors in one init don't prevent the next init from occuring, but that depends on your needs.
Just combine them into one call with an anonymous function:
$(document).ready(function()
{
FlashMessenger.init();
SelectLanguage.init();
});
$(document).ready(function() {
FlashMessenger.init();
SelectLanguage.init();
});
Option 1
FlashMessenger = {
init: function() {
setTimeout(function() {
$(".flash").fadeOut("slow", function () {
$(".flash").remove();
});
}, 5000);
}
}
SelectLanguage = {
init: function() {
$('#selectLanguageId').change(function() {
$('#frmSelectLanguage').submit();
});
}
}
$(function(){
FlashMessenger.init();
SelectLanguage.init();
});
Option 2
FlashMessenger = {
init: function() {
setTimeout(function() {
$(".flash").fadeOut("slow", function () {
$(".flash").remove();
});
}, 5000);
}
}
SelectLanguage = {
init: function() {
$('#selectLanguageId').change(function() {
$('#frmSelectLanguage').submit();
});
}
}
$(document).ready(function(){
FlashMessenger.init();
SelectLanguage.init();
});
Option 3
You actually don't need those 2 objects since the only hold the init methods, so here's the ultimate solution, in my opinion, unless you use those objects elsewhere.
$(function(){
$('#selectLanguageId').change(function() {
$('#frmSelectLanguage').submit();
});
setTimeout(function() {
$(".flash").fadeOut("slow", function () {
$(".flash").remove();
});
}, 5000);
})
I prefer 2 and 3 for this reason.
I think what the op is saying is, "If in the future I have a third function to be invoked at document.ready, then how do I do it without touching that piece of code?"
If you do not want multiple $(document).ready() calls, you could just create an array called startupHooks and add each method to it:
startupHooks[ startupHooks.length ] = myNewStartupHook;
and your startup script could look like
$(document).ready(function() {
for( var i=0; i<startupHooks.length; i++ ) {
startupHooks[i]();
}
}
I know that is not mighty useful, but if that appeals to you, you can do it this way.
Personally, I'd go with multiple $(document).ready() calls.
Personally I'd go for not using document.ready at all.
If you place the scripts at the end of your html-page(just before the tag) you can just write in any way you like.
Maybe this doesn't work for 0.01% of the scripts but it never failed to work for me.
Positive effect of this is that the initial HTML+CSS rendering goes faster.
You can also read about it on yahoo. http://developer.yahoo.com/performance/rules.html#js_bottom

Categories

Resources