简体   繁体   中英

How do I put a time delay into my script?

I want a 3 second delay in my script

However how can I do this, I don't know!

My script:

<script type="text/javascript">

    var baslik = document.title;
    $(document).ready(function () {

        document.title = '(Welcome)' + baslik; // After 3 seconds 
        document.title = '(What can i do for you ?)' + baslik; // After 3 seconds
        document.title = '(Thank u for viewing to me)' + baslik; // After 3 seconds 
        document.title = baslik;
    });         
</script>

use Timeout http://www.w3schools.com/jsref/met_win_settimeout.asp

var baslik = document.title;
postMsg = function(txt) {
   document.title = txt + baslik;
}

$(document).ready(function(){
  setTimeout("postMsg('(Welcome)')",3000);
  setTimeout("postMsg('(What can i do for you ?)')",6000);
  setTimeout("postMsg('(Thank u for viewing to me)')",9000);
});

you can use the setTimeout method

$(document).ready(function(){

     setTimeout(function(){

        var baslik = document.title;    
         document.title = '(Welcome)' + baslik; // After 3 seconds 
         document.title = '(What can i do for you ?)' + baslik; // After 3 seconds 
         document.title = '(Thank u for viewing to me)' + baslik; // After 3 seconds 
         //document.title = baslik;       

    },3000);
});

Something like this will do the trick,

 <script type="text/javascript">

var baslik = document.title;
var welcomeMessages = ['(Welcome)',
                '(What can i do for you ?)',
                '(Thank u for viewing to me)' ];
var timer; 
var msgPt = 0;
$(document).ready(function () {
     timer = setInterval(function () {
         if (msgPt == welcomeMessages.length) {
            clearInterval(timer);
            document.title = baslik;
            return;
         }
         document.title = welcomeMessages[msgPt++];
     }, 3000);             
});


</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM