Results 1 to 7 of 7

Thread: exceed time limit to excecute function

  1. #1
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default exceed time limit to excecute function

    Could anyone help me to solve the problem that exists in my code.
    If the user inputs the command exceeding the time limit, say for example, 7 seconds, the system will deduct his/her marks.

    Yet, my code shows that if the user can input within the limit also deduct mark .
    I hope someone can help me.

    My code is at here.

    Code:
    <HEAD>
    <SCRIPT language="JavaScript">
    <!--
    
    function startclock()
    {
    var thetime=new Date();
    
    var nhours=thetime.getHours();
    var nmins=thetime.getMinutes();
    var nsecn=thetime.getSeconds();
    var AorP=" ";
    
    if (nhours>=12)
        AorP="P.M.";
    else
        AorP="A.M.";
    
    if (nhours>=13)
        nhours-=12;
    
    if (nhours==0)
     nhours=12;
    
    if (nsecn<10)
     nsecn="0"+nsecn;
    
    if (nmins<10)
     nmins="0"+nmins;
    
    document.clockform.clockspot.value=nhours+":"+nmins+":"+nsecn+" "+AorP;
    
    setTimeout('startclock()',1000);
    }
    
    
    var counter1=0; 
    var counter2=0; 
    var counter3=0; 
    var counter4=0;
    var t1 = new Array (15);
    var t2 = new Array (15);
    var t3 = new Array (15);
    var t4 = new Array (15);
    var ranNo;
    var total=10000;
    
    function randomNumber()
    {
    ranNo=Math.floor(1+Math.random()*6);
    document.genNo.randomNum.value=ranNo;
    if (setTimeout('randomNumber()',7000)){
    total=total-1;
    document.calculateCredit.totalListMark.value=total;}
    }
    
    function Submit1(){
    incrementalCount1();}
    
    function Submit2(){
    incrementalCount2();}
    
    function Submit3(){
    incrementalCount3();}
    
    function Submit4(){
    incrementalCount4();}
    
    
    function incrementalCount1()
    {
    var i = 0;
    counter1=counter1+ranNo;
    document.genNo.counting1.value=counter1;
    randomNumber();
    }
    
    function incrementalCount2()
    {
    var i = 0;
    counter2=counter2+ranNo;
    document.genNo.counting2.value=counter2;
    randomNumber();
    }
    
    function incrementalCount3()
    {
    var i = 0;
    counter3=counter3+ranNo;
    document.genNo.counting3.value=counter3;
    randomNumber();
    }
    
    
    function incrementalCount4()
    {
    var i = 0;
    counter4=counter4+ranNo;
    document.genNo.counting4.value=counter4;
    randomNumber();
    }
    
    
    
    </SCRIPT>
    </HEAD>
    <BODY onload = "startclock()" >
    
    <FORM name="clockform">
    Current Time: <INPUT TYPE="text" name="clockspot" size="15">
    </FORM>
    
    <INPUT ID = "1" TYPE = "button" name="start" value = "Start" size="15" onclick = "randomNumber()">
    
    
    <FORM name="genNo">
    Random Number of Customer(s): <INPUT TYPE="text" name="randomNum" size="15"> <br />
    <INPUT ID = "1" TYPE = "button" name="sumbitConf1" value = "Submit1" size="15" onclick = "Submit1()">
    <INPUT ID = "2" TYPE = "button" name="sumbitConf2" value = "Submit2" size="15" onclick = "Submit2()">
    <INPUT ID = "3" TYPE = "button" name="sumbitConf3" value = "Submit3" size="15" onclick = "Submit3()">
    <INPUT ID = "4" TYPE = "button" name="sumbitConf4" value = "Submit4" size="15" onclick = "Submit4()"><br />
    
    
    Table1: <INPUT TYPE="text" name="counting1" size="15"><br>
    Table2: <INPUT TYPE="text" name="counting2" size="15"><br>
    Table3: <INPUT TYPE="text" name="counting3" size="15"><br>
    Table4: <INPUT TYPE="text" name="counting4" size="15"><br>
    </FORM>
    
    <Form name="calculateCredit">
    Total Marks<INPUT TYPE="text" name="totalListMark" size="15"></form>
    
    
    
    </BODY>
    </HTML>
    Last edited by jscheuer1; 02-05-2012 at 08:27 PM. Reason: Format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If the user inputs what command?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    If the user inputs what command?
    if the user presses the "submit" button beyond the time limit, then marks will be deducted, otherwise, marks will be unchanged!!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Yes but there are 4 submit buttons. Do they have to click all 4 within the alloted time or just one?

    Also, I notice that the random numbers keep getting generated. Should this stop happening once the user submits? If so, after any submit, or after all 4 submits?

    Is this a game? If so please explain the rules and basic strategy in a bit more detail. If it's something else, please explain its purpose.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Yes but there are 4 submit buttons. Do they have to click all 4 within the alloted time or just one?

    Also, I notice that the random numbers keep getting generated. Should this stop happening once the user submits? If so, after any submit, or after all 4 submits?

    Is this a game? If so please explain the rules and basic strategy in a bit more detail. If it's something else, please explain its purpose.
    Sorry for replying so late.

    Yes, this is a game.
    1) The restaurant has 4 table and there is a waiter taking charging of assigning people to ONE of the tables ( max ppl in a table = 12).
    2) If the customer is not be served within 5 SECONDS, he gets angry and goes away. Marks will be deducted.
    3) After the customer is assigned to one of the tables in the restaurant, he will dine. Finally, the group of customers leave after 10 seconds. So the vacancy for that table increase.

    My problem is even i put the customer to a table within time limit, mark is deducted as well.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'll assume for now you don't want me to write this for you. I'm not sure if I want to.

    I can tell you that in the code from your post there is no limit on the number of diners per table, the time limit is 7 seconds for seating and there's no time limit on how long the diners remain at a given table.

    Furthermore the problem with the 7 second timer is two or maybe threefold. The first issue is that in this construct:

    Code:
    if (setTimeout('randomNumber()',7000)){
    total=total-1;
    the highlighted always sets the timeout and almost always will evaluate as true. The setTimeout() function returns an object (the handle for the timeout), which is always true. Sometimes in a case like this it can be interpreted as a number though, also always true unless it happens to be 0, which it sometimes can be.

    The timeout's handle is not stored as a reference in a variable or elsewhere, so cannot be canceled once set in motion.

    And it's repeatedly invoked. Once when Start is pressed, again each time one of the submit buttons are pressed. These timeouts all run at once, so at some point you can have one firing off its function (the randomNumber() function) every second or so or even more frequently, most likely in an erratic pattern.

    Before I read your latest post, I assumed this was a game and was playing around with it. I arrived at:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
    function startclock()
    {
    var thetime=new Date();
    
    var nhours=thetime.getHours();
    var nmins=thetime.getMinutes();
    var nsecn=thetime.getSeconds();
    var AorP=" ";
    
    if (nhours>=12)
        AorP="P.M.";
    else
        AorP="A.M.";
    
    if (nhours>=13)
        nhours-=12;
    
    if (nhours==0)
     nhours=12;
    
    if (nsecn<10)
     nsecn="0"+nsecn;
    
    if (nmins<10)
     nmins="0"+nmins;
    
    document.clockform.clockspot.value=nhours+":"+nmins+":"+nsecn+" "+AorP;
    
    setTimeout('startclock()',1000);
    }
    
    
    var counter1=0; 
    var counter2=0; 
    var counter3=0; 
    var counter4=0;
    var t1 = new Array (15);
    var t2 = new Array (15);
    var t3 = new Array (15);
    var t4 = new Array (15);
    var ranNo;
    var total=10000;
    var timer;
    var timers = [];
    
    function randomNumber()
    {
    ranNo=Math.floor(1+Math.random()*6);
    document.genNo.randomNum.value=ranNo;
    if(timer){total=total-1;}
    document.calculateCredit.totalListMark.value=total;
    timer = setTimeout('randomNumber()',7000);
    }
    
    function Submit1(){
    incrementalCount1();}
    
    function Submit2(){
    incrementalCount2();}
    
    function Submit3(){
    incrementalCount3();}
    
    function Submit4(){
    incrementalCount4();}
    
    function resetTimer(){
    	clearTimeout(timer)
    	timer = null;
    	total += ranNo;
    	for (var i = timers.length - 1; i > -1; --i){
    		if(timers.length < 4 || timers[i] < 4){
    			randomNumber();
    			return;
    		}
    	}
    	document.calculateCredit.totalListMark.value=total;
    	var genButs = document.genNo.elements;
    	for (i = genButs.length - 1; i > 0; --i){
    		genButs[i].disabled = genButs[i].type && genButs[i].type === 'button';
    	}
    	document.getElementById('status').innerHTML = 'Game Over - Click "Start" to Begin Again';
    }
    
    function startIt(){
    	clearTimeout(timer);
    	timer = null;
    	total = 1000;
    	var genButs = document.genNo.elements;
    	for (var i = genButs.length - 1; i > 0; --i){
    		genButs[i].disabled = false;
    		if(genButs[i].type && genButs[i].type === 'text'){
    			genButs[i].value = '';
    		}
    	}
    	document.getElementById('status').innerHTML = '';
    	timers = [];
    	randomNumber();
    	counter1=counter2=counter3=counter4=0;
    }
    
    function incrementalCount1()
    {
    if(timers[0] > 3){return;}
    counter1=counter1+ranNo;
    document.genNo.counting1.value=counter1;
    timers[0] = timers[0]? ++timers[0] : 1;
    resetTimer();
    }
    
    function incrementalCount2()
    {
    if(timers[1] > 3){return;}
    counter2=counter2+ranNo;
    document.genNo.counting2.value=counter2;
    timers[1] = timers[1]? ++timers[1] : 1;
    resetTimer();
    }
    
    function incrementalCount3()
    {
    if(timers[2] > 3){return;}
    counter3=counter3+ranNo;
    document.genNo.counting3.value=counter3;
    timers[2] = timers[2]? ++timers[2] : 1;
    resetTimer();
    }
    
    
    function incrementalCount4()
    {
    if(timers[3] > 3){return;}
    counter4=counter4+ranNo;
    document.genNo.counting4.value=counter4;
    timers[3] = timers[3]? ++timers[3] : 1;
    resetTimer();
    }
    
    </script>
    </head>
    <body onload = "startclock()" >
    
    <FORM name="clockform">
    Current Time: <INPUT TYPE="text" name="clockspot" size="15">
    </FORM>
    
    <INPUT TYPE = "button" name="start" value = "Start" onclick = "startIt()">
    
    
    <FORM name="genNo">
    Random Number of Customer(s): <INPUT TYPE="text" name="randomNum" size="15"> <br />
    <INPUT TYPE = "button" name="sumbitConf1" value = "Submit1" onclick = "Submit1()">
    <INPUT TYPE = "button" name="sumbitConf2" value = "Submit2" onclick = "Submit2()">
    <INPUT TYPE = "button" name="sumbitConf3" value = "Submit3" onclick = "Submit3()">
    <INPUT TYPE = "button" name="sumbitConf4" value = "Submit4" onclick = "Submit4()"><br />
    
    
    Table1: <INPUT TYPE="text" name="counting1" size="15"><br>
    Table2: <INPUT TYPE="text" name="counting2" size="15"><br>
    Table3: <INPUT TYPE="text" name="counting3" size="15"><br>
    Table4: <INPUT TYPE="text" name="counting4" size="15"><br>
    </FORM>
    
    <Form name="calculateCredit">
    Total Marks<INPUT TYPE="text" name="totalListMark" size="15"></form>
    
    <div id="status">Click "Start" to Begin</div>
    
    </BODY>
    </HTML>
    The rules are different than yours, but it illustrates getting a handle on the timeout and clearing it if the player responds in time.

    The rules for this game are - You are allowed 4 shots at each table, if you dawdle at any point for 7 seconds you lose a point. Once you have completed all four shots at all four tables, the game is over.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. The Following User Says Thank You to jscheuer1 For This Useful Post:

    newbieCoder (02-07-2012)

  8. #7
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    jscheuer1, i learn a lot from your edition,
    thank you very much!!!!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •