www.webdeveloper.com
Results 1 to 5 of 5

Thread: Beginner question about pass parameter into function, thanks

  1. #1
    Join Date
    Jun 2013
    Posts
    3

    Question Beginner question about pass parameter into function, thanks

    In my asp.net page I have a simple javascript function that will create a count down meter, initial value is set to seconds through "var leave =<%=seconds %>;"
    originally countertimer has no parameter and everything seems to work;
    But I need to add more function there: when the time is up, user will get prompt to either extend the timer or be redirected to another page.

    So I tried to add a variable to the function
    by changing function CounterTimer() to function CounterTimer(a) and
    add a line: var leave = a;

    and it seems not working because the value is not passed to inside the function by calling CounterTimer(60) or CounterTimer("60")

    HTML Code:
    <script type="text/javascript">
    
    var leave =<%=seconds %>;
    
    //CounterTimer(leave);
    CounterTimer();
    var interv=setInterval(CounterTimer,1000);
    
    function CounterTimer()
    {
    //var leave = a;
    var day = Math.floor(leave / ( 60 * 60 * 24))
    var hour = Math.floor(leave / 3600) - (day * 24)
    var minute = Math.floor(leave / 60) - (day * 24 *60) - (hour * 60)
    var second = Math.floor(leave) - (day * 24 *60*60) - (hour * 60 * 60) - (minute*60)
    
    hour=hour<10 ? "0" + hour : hour;
    minute=minute<10 ? "0" + minute : minute;
    second=second<10 ? "0" + second : second;
    
    var remain= hour + ":" + minute+ ":" + second;
    leave=leave-1;
    
    document.getElementById("timelabel").innerHTML =remain;
    if (hour == 00 && minute == 00 && second == 00)
    {
    if(confirm('Do you want to extend your session to another one hour?'))
        {
            CounterTimer(60);
        }
    else{
           window.location="http://www.newlocation.com";//Redirect current page to another page
        }
    }
    }
    
    </script>
    How do I fix it?

    Thank you.

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Countdown</title>
    <meta charset="utf-8" />
    </head>
    <body>
    <center><br /><br /><br /><br /><br /><br /><br /><br /><br /><div id="timelabel"></div></center>
    <script type="text/javascript">
    var leave=5;
    function CounterTimer(){
    var day = Math.floor(leave / ( 60 * 60 * 24)),
    hour = Math.floor(leave / 3600) - (day * 24),
    minute = Math.floor(leave / 60) - (day * 24 *60) - (hour * 60),
    second = Math.floor(leave) - (day * 24 *60*60) - (hour * 60 * 60) - (minute*60);
    
    hour=hour<10 ? "0" + hour : hour;
    minute=minute<10 ? "0" + minute : minute;
    second=second<10 ? "0" + second : second;
    
    document.getElementById("timelabel").innerHTML = hour + ":" + minute+ ":" + second;
    leave--;
    
    if(hour==='00' && minute==='00' && second==='00'){
    if(confirm('Do you want to extend your session to another one hour?')){leave=3600;CounterTimer();}
    else{window.location="http://www.newlocation.com";}
    }
    setTimeout('CounterTimer()',1000);
    }
    CounterTimer();
    </script>
    </body>
    </html>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Jun 2013
    Posts
    3
    Thank you. Can't I make it like CounterTimer(parameters here)?

  4. #4
    Join Date
    Jun 2013
    Posts
    3
    By the way, if after time is up and user did not respond, how can I force the page be redirected to the default page?

    Thanks

  5. #5
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Countdown II</title>
    <meta charset="utf-8" />
    <style type="text/css">
    body{color:#000;background-color:#fff;font-family:'Bookman Old Style',Georgia,Verdana;font-size:14px;text-align:center;padding-top:250px;}
    a:link,a:active,a:visited{text-decoration:none;font-weight:bold;background-color:transparent;color:#000;font-family:'Monotype Corsiva',Georgia,Verdana;font-size:16px;}
    a:hover{text-decoration:none;font-weight:bold;background-color:transparent;color:Crimson;}
    .btn{margin-left:20px;margin-right:20px;}
    #alrt{position:absolute;width:250px;padding:20px 20px;color:#000;background-color:Wheat;border:2px solid #ccc;border-radius:5px;}
    #timelabel{font-size:36px;}
    </style>
    <script type="text/javascript">
    var to='',forcedRedirTimeout=10000;
    function getPositionData(element){var left=element.offsetLeft,top=element.offsetTop;for(var parent=element.offsetParent;parent;parent=parent.offsetParent){left+=parent.offsetLeft;top+=parent.offsetTop;}return{left:left,top:top,width:element.offsetWidth,height:element.offsetHeight};}
    function doc(id){return document.getElementById(id);}
    function CounterTimer(leave){
    var day = Math.floor(leave / ( 60 * 60 * 24)),
    hour = Math.floor(leave / 3600) - (day * 24),
    minute = Math.floor(leave / 60) - (day * 24 *60) - (hour * 60),
    second = Math.floor(leave) - (day * 24 *60*60) - (hour * 60 * 60) - (minute*60);
    
    hour=hour<10 ? "0" + hour : hour;
    minute=minute<10 ? "0" + minute : minute;
    second=second<10 ? "0" + second : second;
    
    doc('timelabel').innerHTML = hour + ":" + minute+ ":" + second;
    leave--;
    
    if(hour==='00' && minute==='00' && second==='00'){clearTimeout(to);msgAlert();}
    else{to=setTimeout('CounterTimer('+leave+')',1000);}
    }
    
    function msgAlert(){
    var div=document.createElement('div');
    div.innerHTML='Do you want to extend your session to another one hour?<br /><br /><a id="yes" href="#null" class="btn">Yes</a><a id="no" href="#null" class="btn">No</a>';
    div.setAttribute('id','alrt');
    document.body.appendChild(div);
    div.style.top=(getPositionData(doc('timelabel'))['top']-div.offsetHeight/2)+'px';
    div.style.left=(document.body.scrollWidth-div.offsetWidth)/2+'px';
    doc('yes').onclick=function(){clearTimeout(to);document.body.removeChild(doc('alrt'));CounterTimer(3600);}
    doc('no').onclick=function(){location.href='http://www.newlocation.com';}
    to=setTimeout("location.href='http://www.newlocation.com';",forcedRedirTimeout);
    }
    
    window.onload=function(){CounterTimer(5);}
    </script>
    </head>
    <body>
    <div id="timelabel"></div>
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    </body>
    </html>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles