Using setTimeout to delay timing of jQuery actions

0 votes

I am attempting to delay the swapping of text in a div. It should operate like a slider/carousel for text.

I must have the code wrong, as the final text replacement never happens.

Also, how would I animate introducing the replacement text (window blinds, for eg.)?


<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />

        <script type="text/javascript">
$(document).ready(function() {

    $("#showDiv").click(function() {
        $('#theDiv').show(1000, function() {
            setTimeout(function() {
                $('#theDiv').html('Here is some replacement text', function() {
                    setTimeout(function() {
                        $('#theDiv').html('More replacement text goes here');
                    }, 2500);
                });
            }, 2500);
        });
    }); //click function ends

}); //END $(document).ready()

        </script>
    </head>
<body>

    Below me is a DIV called "theDiv".<br><br>
    <div id="theDiv" style="background-color:yellow;display:none;width:30%;margin:0 auto;">
        This text is inside the Div called "theDiv".
    </div><br>
    <br>
    <input type="button" id="showDiv" value="Show DIV">



</body>
</html>

May 27, 2022 in Java by Edureka
• 13,690 points
7,981 views

1 answer to this question.

0 votes
The JavaScript setTimeout () function  is used to delay the execution of certain actions or code placed directly in this function or  another JS function. The delay or time is specified in milliseconds with the setTimeout function.
answered May 30, 2022 by gaurav
• 23,260 points

Related Questions In Java

0 votes
1 answer

Using setTimeout to delay timing of jQuery actions

To answer your question, the .html() only ...READ MORE

answered Feb 9, 2022 in Java by Rahul
• 9,680 points
1,882 views
0 votes
1 answer

How to call a method after a delay in Android using Java?

final Handler handler = new Handler(); handler.postDelayed(new Runnable() ...READ MORE

answered Jun 11, 2018 in Java by Akrati
• 3,190 points
5,335 views
0 votes
1 answer

How to get the IP address of the machine using Java?

One approach is to use NetworkInterface.getNetworkInterfaces() to get all ...READ MORE

answered Jun 25, 2018 in Java by sharth
• 3,370 points
978 views
+2 votes
2 answers

How to connect to Oracle using Service Name instead of SID?

HII.. I hope this information may help you- Thin-style ...READ MORE

answered Sep 29, 2020 in Java by SRI
10,097 views
0 votes
1 answer

Best cross-browser method to capture CTRL+S with JQuery?

$(window).keypress(function(event) { if ...READ MORE

answered May 30, 2022 in JQuery by gaurav
• 23,260 points
1,560 views
0 votes
1 answer

jQuery.inArray(), how to use it right?

The jQuery inArray() method is used to find ...READ MORE

answered Jun 1, 2022 in JQuery by Edureka
• 13,690 points
513 views
0 votes
1 answer

Which keycode for escape key with jQuery

27 is the code for the escape key. ...READ MORE

answered Jun 1, 2022 in JQuery by Edureka
• 13,690 points
3,218 views
0 votes
1 answer

JQuery opposite of extend (reduce objects)

There is no such function, but you ...READ MORE

answered Jun 2, 2022 in JQuery by Edureka
• 13,690 points
610 views
0 votes
1 answer

How to set selected value of jQuery Select2?

The third parameter of the new Option ...READ MORE

answered May 30, 2022 in Java by gaurav
• 23,260 points
40,414 views
0 votes
1 answer

Get selected text from a drop-down list (select box) using jQuery

Select elements typically have two values that ...READ MORE

answered May 30, 2022 in Java by gaurav
• 23,260 points
3,210 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP