Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

complicated javascript precondition

Posted on 2009-05-18
10
Medium Priority
?
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
LVL 39

Accepted Solution

by:
abel earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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'…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

718 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