• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

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

0
mattibutt
Asked:
mattibutt
  • 6
  • 4
1 Solution
 
abelCommented:
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/
0
 
mattibuttAuthor Commented:
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
0
 
abelCommented:
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mattibuttAuthor Commented:
waitq contains the question number it is hidden field
 <input name= "waitq" type="hidden" id="waitq"  value="{php}echo $i+1; {/php}"/>
0
 
abelCommented:
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  :)
0
 
abelCommented:
> waitq contains the question number it is hidden field

aha, and you are actually saying that for one specific question, q. nr 1, the timer should be higher. In that case, it is even easier. You do not need all that extra hassle that I put in there to make sure the extra seconds are only added one time. Change the following lines:

var waitMSeconds = addExtraSeconds ? 30 : 0;                  var timeUsed = parseInt(elemTimeUsed.innerHTML) + waitMSeconds;
into the following:

 
var waitMSeconds = addExtraSeconds && document.getElementById("waitq").value == "1" ? 30 : 0;                  var timeUsed = parseInt(elemTimeUsed.innerHTML) + waitMSeconds;

(waitMSeconds stands for waitMoreSeconds, badly chosen name, I know, sorry). And change the following line in the beginning of the script:

var addExtraSeconds = false;
into

var addExtraSeconds = true;
to prevent the extra seconds to be added never, or each time again. If you make these changes, the extra seconds are added once, on load.


-- Abel --
0
 
mattibuttAuthor Commented:
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
0
 
abelCommented:
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"?
0
 
mattibuttAuthor Commented:
thanks buddy for proving extensive help
0
 
abelCommented:
you're welcome, and glad it helped some :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now