Script For Time-Limited Page View

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mercea
    New Member
    • Aug 2007
    • 35

    #1

    Script For Time-Limited Page View

    hi all,
    i'm developing an application were the time to view a certain page is limited. i also want to alert the user when he has 10 more minutes. i'm using this script but it doesn't work well. can anyone help me to check whats wrong and a tip on how to include the alert?:
    Code:
    var countdown = "30"
    function onclick() {
    
             
         if (countdown > 0) {
            countdown-- }
            
            else {
            document.location="Direction.aspx"
            }
            
            window.status=countdown + " minutes left to finish this Paper."
    setTimeout('doCount()',60000)
            }
    onclick()
    thanks guys!
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    Originally posted by mercea
    hi all,
    i'm developing an application were the time to view a certain page is limited. i also want to alert the user when he has 10 more minutes. i'm using this script but it doesn't work well. can anyone help me to check whats wrong and a tip on how to include the alert?:
    Code:
    var countdown = "30"
    function onclick() {
    
             
         if (countdown > 0) {
            countdown-- }
            
            else {
            document.location="Direction.aspx"
            }
            
            window.status=countdown + " minutes left to finish this Paper."
    setTimeout('doCount()',60000)
            }
    onclick()
    thanks guys!
    for an alert at 10 minutes try this:
    [code=javascript]
    var countdown = "30"
    function onclick() {


    if (countdown > 0) {
    countdown-- }
    else if(countdown == 10)
    {
    alert("10 min left");
    }
    else {
    document.locati on="Direction.a spx"
    }

    window.status=c ountdown + " minutes left to finish this Paper."
    setTimeout('doC ount()',60000)
    }
    onclick()

    [/code]

    good luck

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Changed the thread title. Please do not use words such as "Pls" and "help" in the title. Thanks.

      Comment

      • mercea
        New Member
        • Aug 2007
        • 35

        #4
        Originally posted by epots9
        for an alert at 10 minutes try this:
        [code=javascript]
        var countdown = "30"
        function onclick() {


        if (countdown > 0) {
        countdown-- }
        else if(countdown == 10)
        {
        alert("10 min left");
        }
        else {
        document.locati on="Direction.a spx"
        }

        window.status=c ountdown + " minutes left to finish this Paper."
        setTimeout('doC ount()',60000)
        }
        onclick()

        [/code]

        good luck
        thanks but i tried it before and it'll work for the first one minute then say error on page!

        what else can i do?
        thanks

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by mercea
          thanks but i tried it before and it'll work for the first one minute then say error on page!

          what else can i do?
          thanks
          60000 means 60 thousand milliseconds where one millisecond = 1 second, so 60000 is one minute.

          Comment

          • akristensen
            New Member
            • Aug 2007
            • 6

            #6
            --see answer below -- sorry for this bad post

            Comment

            • akristensen
              New Member
              • Aug 2007
              • 6

              #7
              Originally posted by mercea
              thanks but i tried it before and it'll work for the first one minute then say error on page!

              what else can i do?
              thanks
              It seems in your onclick() function you have a setTimeout to call itself in 60 seconds.
              Unless you have doCount() defined elsewhere, you need to call the correct function within the setTimeout.
              Please change your line 13 in your original post or line 16 in epots9 reply to the following and it should work as expected.
              [code=javascript]
              setTimeout('onclick()',60000)
              [/code]

              Comment

              • mercea
                New Member
                • Aug 2007
                • 35

                #8
                Originally posted by akristensen
                It seems in your onclick() function you have a setTimeout to call itself in 60 seconds.
                Unless you have doCount() defined elsewhere, you need to call the correct function within the setTimeout.
                Please change your line 13 in your original post or line 16 in epots9 reply to the following and it should work as expected.
                [code=javascript]
                setTimeout('onclick()',60000)
                [/code]
                hi,
                thanks a lot! i dont know how i missed that. it works well except that using
                [CODE=javascript]var countdown = "31"
                function onclick() {

                if (countdown > 0) {
                countdown-- }
                else if
                (countdown == 10)
                {
                alert("YOU HAVE 10 MINS LEFT!");
                }

                else {
                document.locati on="Direction.a spx"
                }

                window.status=c ountdown + " minutes left to finish this Paper."
                setTimeout('onc lick()',60000)
                }
                onclick()[/CODE]
                it doesnt display the alert.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by mercea
                  hi,
                  thanks a lot! i dont know how i missed that. it works well except that using
                  [CODE=javascript]var countdown = "31"
                  function onclick() {

                  if (countdown > 0) {
                  countdown-- }
                  else if
                  (countdown == 10)
                  {
                  alert("YOU HAVE 10 MINS LEFT!");
                  }

                  else {
                  document.locati on="Direction.a spx"
                  }

                  window.status=c ountdown + " minutes left to finish this Paper."
                  setTimeout('onc lick()',60000)
                  }
                  onclick()[/CODE]
                  it doesnt display the alert.
                  Please use CODE tags when posting code. Thanks.

                  If you look at your if statement, it checks if the countdown is greater than 0. 10 is also greater than 0, so it never enters the else-if statement. To avoid this, first check for 10, then for greater than 0, or move the check for 10 outside this if statement and put it on its own, e.g.
                  [code=javascript] if (countdown > 0) {
                  countdown--; }
                  else {
                  document.locati on="Direction.a spx";
                  }
                  if (countdown == 10)
                  {
                  alert("YOU HAVE 10 MINS LEFT!");
                  }[/code]

                  Comment

                  • mercea
                    New Member
                    • Aug 2007
                    • 35

                    #10
                    Originally posted by acoder
                    Please use CODE tags when posting code. Thanks.

                    If you look at your if statement, it checks if the countdown is greater than 0. 10 is also greater than 0, so it never enters the else-if statement. To avoid this, first check for 10, then for greater than 0, or move the check for 10 outside this if statement and put it on its own, e.g.
                    [code=javascript] if (countdown > 0) {
                    countdown--; }
                    else {
                    document.locati on="Direction.a spx";
                    }
                    if (countdown == 10)
                    {
                    alert("YOU HAVE 10 MINS LEFT!");
                    }[/code]
                    thank u very much.it works perfectly! my application is actually like a Q&A site were the user has limited time within which to answer the questions. is it possible for the time limit to be varied? for instance the site administrator wants to increase the time to 40mins. how can this be acheived without him having to rewrite the code?

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Originally posted by mercea
                      thank u very much.it works perfectly! my application is actually like a Q&A site were the user has limited time within which to answer the questions. is it possible for the time limit to be varied? for instance the site administrator wants to increase the time to 40mins. how can this be acheived without him having to rewrite the code?
                      Hmm.. you shouldn't do this on the client-side. I could easily view the source code and change the limit to 1 hour or however many minutes I wish.

                      You should set this on the server-side. What server-side language are you using for the form processing?

                      Comment

                      • mercea
                        New Member
                        • Aug 2007
                        • 35

                        #12
                        Originally posted by acoder
                        Hmm.. you shouldn't do this on the client-side. I could easily view the source code and change the limit to 1 hour or however many minutes I wish.

                        You should set this on the server-side. What server-side language are you using for the form processing?
                        yeah i actually thought that leaving it that way would allow for changes to be made.i'm using ASP.net. so, i could set a variable parameter,serve r side, that the script will read at run-time. right?

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Originally posted by mercea
                          yeah i actually thought that leaving it that way would allow for changes to be made.i'm using ASP.net. so, i could set a variable parameter,serve r side, that the script will read at run-time. right?
                          For something like this, it would be better to do this on the server-side. Perhaps a time should be set from the server with session variables identifying each user. Each question could be retrieved from the server, so that a check can be made for the time or you could periodically update the time from the server. You could use Ajax for this to avoid having to refresh the page each time.

                          You can combine both server-side and client-side checking, but don't depend on Javascript otherwise it would be too easy to flout the restrictions.

                          These are some suggestions.

                          Comment

                          Working...