最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I put a time delay into my script? - Stack Overflow

programmeradmin4浏览0评论

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 = '(Wele)' + 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>

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 = '(Wele)' + 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>
Share Improve this question edited Feb 23, 2012 at 10:04 chrisbunney 5,9467 gold badges51 silver badges70 bronze badges asked Feb 22, 2012 at 17:29 CWOmerCWOmer 1112 gold badges2 silver badges11 bronze badges 2
  • 3 Hi, your question has been downvoted a bit, this is probably because you've not explained what you've already tried to solve the problem. Even if you've no idea what code to write, have you tried Googling to find useful information? If so, including that into your question will make it a better question. The right queries make all the difference, and I see that originally you asked about a "certain time interval", which would normally be called a "time delay" – chrisbunney Commented Feb 23, 2012 at 10:05
  • 3 I have removed your down votes, but be sure that next time when you ask any questions, make it detailed.. add maximum information and make it clear. Be careful while using this site. – Sarin Jacob Sunny Commented Feb 24, 2012 at 9:31
Add a ment  | 

3 Answers 3

Reset to default 9

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

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

$(document).ready(function(){
  setTimeout("postMsg('(Wele)')",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 = '(Wele)' + 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 weleMessages = ['(Wele)',
                '(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 == weleMessages.length) {
            clearInterval(timer);
            document.title = baslik;
            return;
         }
         document.title = weleMessages[msgPt++];
     }, 3000);             
});


</script>
发布评论

评论列表(0)

  1. 暂无评论