9 Replies Latest reply on Jan 19, 2010 1:39 PM by DPSwebmaster

    need help modifying code for timer

    DPSwebmaster Level 1

      Hi

       

      I am hoping this is fairly easy. I have some code for a timer that counts down from 60 seconds. In order to to start the countdown, a start button is pressed and once the count down begins, there is a stop button. I want to alter the code so that the timer begins automatically (I will be embedding into a web page). I know that to hide a button, I just need to changevisible=true, to visible=false. I basically just need to know hot to get it to start automatically.

       

      Current code here.

       

       

      clock_val = "60:00"
      _root.old_time = "0"

       

      //fscommand("showmenu","false")

       

      my_milli = 0
      old_time = 0

       

      function is_digit(x) {
          var digits = "0123456789."
          if(digits.indexOf(x) >= 0 ) {
              return true
          } else {
              return false
          }
      }
      function first_digit_bunch(x) {
          var x_len = x.length
          var xresult = ""
          var yresult = ""
          var xmode = 0
          var count_3 = 0
          for(i = 0;i<x_len;i++) {
              var xchar = x.charAt(i)
             
         
              if(xmode == 0) {
                  if(is_digit(xchar)) {
                      xmode = 1
                  }
              }
             
              if(xmode == 1) {
                  if(is_digit(xchar)) {
                      xresult += xchar
                  } else {
                      xmode = 2
                  }
              }
             
              if(xmode == 2) {
                  if(xchar == ":") {
                      xmode = 3
                      count_3 = i
                  } else {
                      xmode = 9
                  }
              }
              if(xmode == 3 && i>count_3) {
                  if(is_digit(xchar)) {
                      yresult += xchar
                  } else {
                      xmode = 4
                  }
              }
             
          }
          if(xresult == "") {
              xresult = "0"
          }
          if(yresult == "") {
              yresult = "0"
          }
          var zresult = Math.round(parseFloat(xresult)*60 +impose_range(parseFloat(yresult),0,59.99))/60
          return zresult
      }
      function extract_min() {
          var minutes0
          //if(!_root.is_set) {
              //minutes0 = 5
              //_root.num_minutes = 5
              //_root.is_set = true
          //} else {
             
          if(_root.paused_time != clock_val) { //edited
              minutes0 = first_digit_bunch(clock_val)
          //}
          } else {
              minutes0 = _root.num_minutes
          }
          if(minutes0 <= 0) {
              minutes0 = _root.num_minutes // reset to last set value
          }
          minutes0 = impose_range(minutes0,0,99.99)
          _root.num_minutes = minutes0
          _root.num_seconds = _root.num_minutes*60
          display_time()
      }

       

      function startTimer() {
          extract_min()
          stopTimer()
          //_root.milli_value = my_date.getTime()
          _root.my_milli = (new Date()).getTime()
          _root.old_time = _root.my_milli
          btn_stop._visible = true
          btn_start._visible = false
          msg_stopped._visible = false
          //_root["timer0"+xmodule].gotoAndPlay(2)   
          _root["timer00"].gotoAndPlay(2)   
      }
      function stopTimer() {
          //clock_val = _root.num_minutes + ":00"
          stopTimer0()
          extract_min()
         
         
      }
      function stopTimer0() {
          _root.timer00.gotoAndStop(1)
          display_time()
          btn_stop._visible = false
          btn_start._visible = true
          times_up._visible = false
          msg_stopped._visible = true
          _root.paused_time = clock_val
          //_root.timer01.stop()
          //_root.timer02.stop()
          //_root.timer03.stop()
      }

       

      function endTimer() {
          stopTimer()
          times_up._visible = true
          msg_stopped._visible = false
          the_audio.gotoAndPlay(2)
      }

       

       

       

      function decrement_seconds() {
          //_root.num_seconds--
          _root.old_time = _root.my_milli
          if(_root.my_milli == 0) {
              _root.my_milli = (new Date()).getTime()
          }
          _root.my_milli = (new Date()).getTime()
          var my_diff = _root.my_milli - _root.old_time
          num_seconds -= (my_diff/1000)
          alert("num_seconds = "+num_seconds)
         
      }

       

      function calc_time(cur_frame,end_frame) {
          var frames_remaining = end_frame - cur_frame
          var secs_remaining =  _root.num_seconds    //frames_remaining / 5 // assuming the fps is 5
          trace("secs_remaining=" + secs_remaining)
          var display_result = seconds_to_minsec(secs_remaining)
          return display_result
      }
      function impose_range(x,lower,upper) {
          if(x < lower) {
              return lower
          }
          if(x > upper) {
              return upper
          }
          return x
      }
      function seconds_to_minsec(secs) {
          var mins_val = Math.floor(secs / 60)
         
          mins_val = impose_range(mins_val,0,99.99)
         
          var secs_val = Math.floor(secs % 60)
          secs_val = impose_range(secs_val,0,59)
          var result =  x_digit_string(mins_val,2)  + ":" + x_digit_string(secs_val,2)
          trace("result="+result)
          return result
      }

       

      function x_digit_string (pnumber,x) {
          //no worry about numbers bigger than 999 or negative numbers
          var vstring = String (pnumber)
          return x_fill_zero(vstring,x)
      }

       


      function x_fill_zero(str,len) {
          //will not shorten
          var vresult_length = len
          var vstring_length = str.length
          var gap = vresult_length-vstring_length
          var vresult = str
          for (var i=0;i<gap;i++){
              vresult="0"+vresult
              }
          return vresult
      }

       

             
      //function start_timer_begin_exam() {   
      //    _root.controller9.gotoAndStop("start")   
      //}
             
      function display_time() {
          var clock_hexagesimal = calc_time(_root.num_seconds,_root.num_minutes*60)
          _root.clock_val = clock_hexagesimal
          trace(clock_hexagesimal)
          //clock_num = clock_num + 1   
      }