admin管理员组文章数量:1394049
I have
<script>
function myfunc(){
alert("show this alert in 5000ms after toggleClass executed")
//some code goes here
}
$(document).ready(function(){
$('.mybtn').on('click',function(){
$('#mydiv').toggleClass('newclass');
});
});
</script>
How to execute myfunc()
in 5000ms after toggleClass was executed? Tried several ways but with no luck.
Thank you.
I have
<script>
function myfunc(){
alert("show this alert in 5000ms after toggleClass executed")
//some code goes here
}
$(document).ready(function(){
$('.mybtn').on('click',function(){
$('#mydiv').toggleClass('newclass');
});
});
</script>
How to execute myfunc()
in 5000ms after toggleClass was executed? Tried several ways but with no luck.
Thank you.
Share Improve this question asked Dec 23, 2013 at 9:22 HaradzieniecHaradzieniec 9,34833 gold badges122 silver badges227 bronze badges5 Answers
Reset to default 7use setTimeout()
function myfunc() {
alert("show this alert in 5000ms after toggleClass executed")
//some code goes here
}
$(document).ready(function () {
$('.mybtn').on('click', function () {
$('#mydiv').toggleClass('newclass');
setTimeout(myfunc, 5000)
});
});
Try this...
function myfunc(){
alert("show this alert in 5000ms after toggleClass executed");
//some code goes here
}
$(document).ready(function(){
$('.mybtn').on('click',function(){
$('#mydiv').toggleClass('newclass').delay(5000).queue(myfunc);
});
});
JSFIDDLE DEMO
Use settimeout function
function myfunc() {
alert("show this alert in 5000ms after toggleClass executed")
//some code goes here
}
$(document).ready(function () {
$('.mybtn').on('click', function () {
$('#mydiv').toggleClass('newclass');
setTimeout(myfunc, 5000)
});
});
You can have a callback function like this
$('#mydiv').toggleClass('newclass', 5000).promise().done(function(){alert("done")});
Here is a working demo
I would do the $("#mydiv").toggleClass('newclass')
in a separate event.
For instance <button class="mybtn" onmousedown="$('#mydiv').toggleClass('newclass')" onclick="myfunc();">Fast Toggle</button>
If you toggle onmousedown the page will render with the toggle before the click event fires.
本文标签: javascriptjQuery execute a function AFTER toggleClass was executedStack Overflow
版权声明:本文标题:javascript - jQuery: execute a function AFTER toggleClass was executed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744086181a2588563.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论