Solved

complicated javascript precondition

Posted on 2009-05-18
10
303 Views
Last Modified: 2012-05-07
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
Comment
Question by:mattibutt
  • 6
  • 4
10 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24410185
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
 
LVL 11

Author Comment

by:mattibutt
ID: 24410260
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
 
LVL 39

Expert Comment

by:abel
ID: 24410709
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
 
LVL 11

Author Comment

by:mattibutt
ID: 24410724
waitq contains the question number it is hidden field
 <input name= "waitq" type="hidden" id="waitq"  value="{php}echo $i+1; {/php}"/>
0
 
LVL 39

Expert Comment

by:abel
ID: 24410727
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24410766
> 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
 
LVL 11

Author Comment

by:mattibutt
ID: 24410769
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
 
LVL 39

Expert Comment

by:abel
ID: 24410864
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
 
LVL 11

Author Closing Comment

by:mattibutt
ID: 31582516
thanks buddy for proving extensive help
0
 
LVL 39

Expert Comment

by:abel
ID: 24413247
you're welcome, and glad it helped some :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now