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,716
    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>
    signature under construction

  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,716
    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>
    signature under construction

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11856 seconds
  • Memory Usage 2,908KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)bbcode_html
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates