SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to get an alert to continue every 10 seconds.

    Hello, I have this below which causes an alert after 10 seconds. How would I keep the alert going every 10 seconds after? Please let me know, thank you very much.

    <script type="text/javascript">
    //If the time is less than 10,
    //you will get a "Good morning" greeting.
    //Otherwise you will get a "Good day" greeting.

    var d = new Date();
    var time = d.getSeconds();

    if (time > 10)
    {
    alert("Hello day!")
    }
    </script>
    </body>
    </html>

  2. #2
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I doubt it can be exactly 10 seconds. Alert will hook down the page and keep waiting for user to click 'OK'. unless you use modal window.

  3. #3
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats ok if its not 10 exactly I'm just trying to get it to work. Around 20 seconds or more after ok is clicked on each time is fine. Anyone know?

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should do what you are after
    Code:
    <script type="text/javascript" charset="utf-8">		
    	var Alerter = {
    		Wait : 10, //Default alert to 10 seconds
    		Timer : null,
    		Init : function(){
    			this.Timer = setTimeout("Alerter.Alert()", this.Wait * 1000);
    		},
    		Alert : function(){
    			alert('alert');
    			this.Timer = setTimeout("Alerter.Alert()", this.Wait * 1000);
    		}
    	};
    	
    	Alerter.Init();
    	
    </script>

  5. #5
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You nailed it Mortimer!! I'm trying to understand it though so I can adjust it if needed. I've never seen javascript with these before....Timer :
    Words with colons, what is that? Please let me know, thank you very much.

  6. #6
    code ninja hamstu's Avatar
    Join Date
    Nov 2002
    Location
    Montréal, Canada
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 1Jen View Post
    You nailed it Mortimer!! I'm trying to understand it though so I can adjust it if needed. I've never seen javascript with these before....Timer :
    Words with colons, what is that? Please let me know, thank you very much.
    It's a JavaScript Object. It's somewhat similar to an Array, but you give names to each of the different elements, and then use that name to reference them later. The colon acts like the '=' assignment operator. For example:
    Code:
    var obj = 
    {
        name : "A string",
        showAlert : function( ) { alert("You can use functions too."); }
    }
    alert(obj.name);
    obj.showAlert();

  7. #7
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for explaining, I'll study it and see if it makes sense to me, thanks.

  8. #8
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's delving into the deep end of JavaScript...right into OO from the word go... I suppose one learns quicker that way. That's a nice little "mini" Alerter class.
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •