Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Page Refreshes before Timer has done it's job...

    I'm just trying to make a simple interface.

    On the html form the user enters the time to start, time to end, and frequency of a reminder alert.

    I pass this to my javascript and then am trying to setup a 1 second loop where it checks against the clock to see if it's time to begin the reminders.

    I get to the function, but the setTimeout doesn't complete and my page just refreshes.

    Here's my javascript code for this section....

    Code:
    //-----------Start Countdown---------------------------
    function StartCountDown(startTime, endTime, freq){
    	now=new Date();
    	intToStart=startTime-now;
    	intInSec=intToStart/1000;
    	alert(intInSec + " sec");
    	if (now > startTime){
    		StartRepeat(endTime, Freq);
    		}
    	else{
    		var TTS=setTimeout("StartCountDown(startTime, endTime, freq);", 1000);
    		}
    }
    
    function StartRepeat(endTime, freq){alert("Made it to StartRepeat Function");
    	if (freq != 0){
    		var task=setInterval("TaskRemind(endTime, freq)", freq);
    		}
    	else{
    		clearTimeout(task);
    		}
    }
    
    function TaskRemind(endTime, freq){
    	alert("Do Task Now!");
    	
    	now=new Date();
    	
    	if (now <= endTime){
    		StartRepeat(endTime, freq);
    		}
    	else{
    		freq=0;
    		StartRepeat(endTime, freq);
    		}
    }
    Any help is greatly appreciated.

    Brian

  • #2
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    in your form tag add onsubmit="return(false)"

  • #3
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    set the return(false) in the <form> tag...and it doesn't refresh anymore, but now I get to about the same spot, and it waits a few seconds then pops up with an alert box that I didn't make which says:

    True!

    It has a yellow triangle with an exclamation point in it.

    Any ideas are welcome.

    Is there some limit on the time that a timer or interval can be set at?

  • #4
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    can you post the HTML too (and any other js on the page)

  • #5
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I was trying to keep the code posted minimal as instructed by the 'Read These First' topics, but okay...

    HTML Code
    Code:
    <!DOCTYPE html>
    <html>
    
    <head>
       <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
       <meta name="apple-mobile-web-app-capable" content="yes" />
       <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    
    <title>RepeaTask</title>
    <link rel="stylesheet" type="text/css" href="RepeaTask.css" />
    
    <script type="text/javascript" src="RepeaTask.js"></script>
    
    </head>
    
    <body>
       <form onsubmit="return(false)">
    	<h1>RepeaTask</h1>
    	<div class="intro">
    		<p>Enter the Start Time for your repeated task, then enter the end time.  You can enter a repeat frequency in hours, minutes, or seconds.  If you do not enter a repeat frequency, the default frequency of 15 minutes will be used.
    		</p>
    	</div>
    	
    	<div class="timeform">
    	Enter Start Time:
    	</div>
    	
    	<div>
    	<input type="number" name="STimeH" size="2" minimum="01" maximum="12" step="1" placeholder="01" />:
    	<input type="number" name="STimeM" size="2" minimum="00" maximum="59" step="1" placeholder="00" />
    	AM or PM: <input type="text" name="StartAorP" size="2" placeholder="PM"/>
    	</div>
    
    	<div>
    	Enter End Time:
    	</div>
    	<div>
    	<input type="number" name="ETimeH" size="2" minimum="01" maximum="12" step="1" placedholder="01" />:
    	<input type="number" name="ETimeM" size="2" minimum="00" maximum="59" step="1" placedholder="00" />
    	AM or PM<input type="text" name="EndAorP" size="2" placeholder="PM" />
    	</div>
    
    	<div>
    	Enter Repeat Frequency in hh:mm:ss :
    	</div>
    	<div>
    	<input type="number" name="FreqH" size="2" placeholder="00" />:
    	<input type="number" name="FreqM" size="2" placeholder="00" />:
    	<input type="number" name="FreqS" size="2" placeholder="00" />
    	</div>
    	<div><br></div>
    
    	<div>
    	<button type="submit"  name="Set Repeat Alert" onclick="SetTimer(STimeH, STimeM, StartAorP, ETimeH, ETimeM, EndAorP, FreqH, FreqM, FreqS)">Set Repeat Alert</button>
    	<input type="button" value="Cancel Alert" onclick="CancelTimer()" />
    	</div>
    
    </form>
    </body>
    </html>
    And here's the javascript code. Keep in mind, I"m a beginner, so there are probably much easier ways to do what I'm doing, and feel free to point them out.

    JS Code
    Code:
    //RepeaTask.js   This is an initial startup
    
    //Pull in User Entered Data
    
    var q;
    var timer_is_on=0;
    var stimeh;
    var stimem;
    var etimeh;
    var etimem;
    var freqh;
    var freqm;
    var freqs;
    var startaorp;
    var endaorp;
    var startTime;
    var endTime;
    var intToStart;
    var freq;
    
    //--------------------------------------------------------------------------//
    function SetTimer(STimeH, STimeM, StartAorP, ETimeH, ETimeM, EndAorP, FreqH, FreqM, FreqS){
    	stimeh=STimeH.value;
    	stimem=STimeM.value;
    	etimeh=ETimeH.value;
    	etimem=ETimeM.value;
    	freqh=FreqH.value;
    	freqm=FreqM.value;
    	freqs=FreqS.value;
    	startaorp=StartAorP.value;
    	endaorp=EndAorP.value;
    	startTime=new Date();
    	endTime=new Date();
    	
    	stimeh=stimeh*1;
    	etimeh=etimeh*1;
    	stimem=stimem*1;
    	etimem=etimem*1;
    	freqh=freqh*1;
    	freqm=freqm*1;
    	freqs=freqs*1;
    	freq=(freqh*3600000)+(freqm*60000)+(freqs*1000);
    
    //----------  Check AM or PM and convert to Military time if needed --------//
    if (startaorp == "pm"){
    	stimeh+=12;
    	}
    
    if (endaorp =="pm"){
    	etimeh+=12;
    	}
    
    //-----------Set User Input in Time Format --------------------------------//
    startTime.setHours(stimeh);
    startTime.setMinutes(stimem);
    startTime.setSeconds(0);
    startTime.setMilliseconds(0);
    endTime.setHours(etimeh);
    endTime.setMinutes(etimem);
    endTime.setSeconds(0);
    endTime.setMilliseconds(0);
    
    CalcTimeInt(startTime, endTime, freq);
    }
    
    //-----------Calculate the Time Interval-----------------------------------//
    function CalcTimeInt(startTime, endTime, freq){
    
    now = new Date();
    intToStart=startTime-now;
    
    var hours=(intToStart/3600000);
    var hrem=(intToStart%3600000);
    var h=parseInt(hours);
    
    var min=(hrem/60000);
    var m=parseInt(min);
    var mrem=(intToStart%60000);
    
    var sec=(mrem/1000);
    var s=parseInt(sec);
    
    alert("Now is " + now);
    alert("Repeat Task Alerts will start in " + h + " Hrs " + m + " Min " + s + " Sec");
    StartCountDown(startTime, endTime, freq);
    }
    
    
    //-----------Start Countdown--------------------------------------------------//
    function StartCountDown(startTime, endTime, freq){
    	now=new Date();
    	intToStart=startTime-now;
    	intInSec=intToStart/1000;
    	if (now > startTime){
    		StartRepeat(endTime, Freq);
    		}
    	else{
    		var TTS=setTimeout("StartCountDown(startTime, endTime, freq);", 1000);
    		}
    }
    
    function StartRepeat(endTime, freq){alert("Made it to StartRepeat Function");
    	if (freq != 0){
    		var task=setInterval("TaskRemind(endTime, freq)", freq);
    		}
    	else{
    		clearTimeout(task);
    		}
    }
    
    function TaskRemind(endTime, freq){
    	alert("Do Task Now!");
    	
    	now=new Date();
    	
    	if (now <= endTime){
    		StartRepeat(endTime, freq);
    		}
    	else{
    		freq=0;
    		StartRepeat(endTime, freq);
    		}
    }
    Thanks, again for all the help,

  • #6
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    in line 95 you need to make freq lowercase
    Code:
    function StartCountDown(startTime, endTime, freq){
    	now=new Date();
    	intToStart=startTime-now;
    	intInSec=intToStart/1000;
    	if (now > startTime){
    		StartRepeat(endTime, freq);//made f ing freq lowercase
    		}
    	else{
    		var TTS=setTimeout("StartCountDown(startTime, endTime, freq);", 1000);
    		}
    }
    i wasn't able to duplicate your problem with the true! alertbox.
    in the future use your browsers developer tools when you write JavaScript. it will often tell you where the problem is


  •  

    Tags for this Thread

    Posting Permissions

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