Link to home
Start Free TrialLog in
Avatar of mattibutt
mattibuttFlag for United States of America

asked on

complicated javascript precondition

hi
i have a timer which starts as soon quiz page load but now i have created a delay of five seconds i want the timer to add additional time if it meets the condition.
waitp is a new declared variable if the waitq textbox contain value 1 then it should apply the condition i have tried different things but nothing seems to be working
  var waitp   = parseInt(document.getElementById('waitq').value);
//currently i am trying the following but it doesn't work
<script type='text/javascript'>
  var counter   = document.getElementById( 'quiz_time_unused' )
    var waitp   = parseInt(document.getElementById('waitq').value);
  counter.value = '1000'
  var interval  = 20

  var timerID   = null
 
  function display(){
    var val = parseInt( counter.value )
               if (waitp == 1)
   { var val =  parseInt( counter.value)  }
    val -= 1

    counter.value = val


   if ( val == 50 ) {
      clearTimeout( timerID )
    }
  }
  timerID = setInterval( "display()", 20 )
</script>


//original code
<script type='text/javascript'>
  var counter   = document.getElementById( 'quiz_time_unused' )
  counter.value = '1000'
  var interval  = 20
  var timerID   = null
 
  function display(){
    var val = parseInt( counter.value )
    val -= 1
    counter.value = val
    if ( val == 50 ) {
      clearTimeout( timerID )
    }
  }
  timerID = setInterval( "display()", 20 )
</script>

Open in new window

Avatar of abel
abel
Flag of Netherlands image

Do you remember you asked this already? Here: http://Q_24411674.html and the page that was offline during the weekend to show you how to add seconds to the page is online again: http://www.undermyskin.org/external-examples/ee-questions/Q_24411674/
Avatar of mattibutt

ASKER

hi abel
i see that page now the thing is currently the way your timer is done it requires a click but i want to do this onpage load when waitq = 1
No problem. Just change this line:

document.getElementById("waitq").checked = false;

into this line:

document.getElementById("waitq").checked = true;

(and the current body-onload is called setUnchecked, if you change it, the meaning changes, so you may want to change the name into setChecked instead).

Why do you say waitq = 1? Is it something else then a checkbox? If so, can you show the declaration of the HTML of the waitq element?
waitq contains the question number it is hidden field
 <input name= "waitq" type="hidden" id="waitq"  value="{php}echo $i+1; {/php}"/>
There is, btw, a very important difference between my approach and your approach. I use a one-off timer (the setTimeout() function) instead of a repeating timer (the setInterval() function you use). It is a matter of taste, but I find the setInterval approach very cumbersome and you are always in trouble whenever you want to control the timeouts. The approach presented here is easier to understand and less errorprone I believe. Hopefully I made the code understandable  :)
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i agree with you but that's how my boss wants it what can you do when you are trapped in the situation like me
nothing much ;-)

But you are not meaning to say that your "boss" is telling you what technique to use for implementation (never heard of such bosses)? Or are you saying that this is actually homework and that the assignment says "use setInterval"?
thanks buddy for proving extensive help
you're welcome, and glad it helped some :)