2013-06-05 3 views
22

मैं एक div में पाठ के स्वैपिंग में देरी करने का प्रयास कर रहा हूं। यह पाठ के लिए एक स्लाइडर/कैरोसेल की तरह काम करना चाहिए।jQuery क्रियाओं के समय में देरी करने के लिए setTimeout का उपयोग

मेरे पास कोड गलत होना चाहिए, क्योंकि अंतिम पाठ प्रतिस्थापन कभी नहीं होता है।

इसके अलावा, मैं प्रतिस्थापन पाठ (उदाहरण के लिए विंडो अंधा,) को कैसे शुरू कर सकता हूं?


<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> 
+0

सबसे उपयुक्त इसके लिए टूल '.queue() 'मुझे लगता है ... – Shikkediel

उत्तर

24

.html() केवल एक स्ट्रिंग या एक तर्क, not both के रूप में एक समारोह लेता है। इस प्रयास करें:

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

jsFiddle example

+0

क्या होगा यदि मैं उस div के अंदर डालता हूं .. क्या स्क्रिप्ट भी ताज़ा हो जाएगी? या कुछ और। – Vincent

2

तुम भी setTimeout() के बजाय jQuery's delay() method उपयोग कर सकते हैं। यह आपको बहुत अधिक पठनीय कोड देगा। यहाँ डॉक्स से एक उदाहरण है:

$("#foo").slideUp(300).delay(800).fadeIn(400); 

केवल सीमा (है कि मैं के बारे में पता कर रहा हूँ) है कि यह आपको समय समाप्ति स्पष्ट करने के लिए एक तरह से नहीं देता है। यदि आपको ऐसा करने की ज़रूरत है तो आप सभी नेस्टेड कॉलबैक के साथ चिपके रहेंगे जो setTimeout आपके ऊपर जोर देते हैं।

+0

असल में, आप अभी तक निष्पादित नहीं किए गए किसी भी एनीमेशन को हटाने के लिए 'clearQueue()' का उपयोग कर सकते हैं। – Shikkediel

7

इस प्रयास करें:

function explode(){ 
    alert("Boom!"); 
} 
setTimeout(explode, 2000); 
0

यह कैसे मैं इस समस्या मेनू माउस बाहर (है कि अगर मंडराना आग नहीं था) के बाद कुछ ही सेकंड बंद कर देता है हल है,

$setM_swith=0; //Set timer switch 
$(function(){ 
     $(".navbar-nav li a").click(function(event) { 
      if (!$(this).parent().hasClass('dropdown')) 
       $(".navbar-collapse").collapse('hide'); 
     }); 
     $(".navbar-collapse").mouseleave(function(){ 
      $setM_swith=1; 
      setTimeout(function(){ if($setM_swith==1){$(".navbar-collapse").collapse('hide');$setM_swith=0;} }, 3000); 
     }); 
     $(".navbar-collapse").mouseover(function(){ 
      $setM_swith=0; 
     }); 
    }); 
+0

क्षमा करें गलत स्थान :( –

संबंधित मुद्दे