problem with recording the time on button click
I have a experiment of image slideshow, where each image slides for every three seconds. I have two buttons one is called "Pause bag" and other "Pass bag", and within that three seconds I need to click on "Pause bag" button. As I click on the "Pause bag" the image gets paused and when I click on "Pass bag" Slideshow resumes (another image is displayed on the screen).
My Problem is I need to record number of seconds for the paused image. As soon as I click on the "Pause bag" button, a function called btn_pause_click()
is invoked, this function records the number of seconds for the image pause till the "Pass bag" is clicked. For the first click of "Pass bag" I'm getting the exact number of seconds for image pause, then slide show resumes and another image is seen on the screen. When I again click on it I'm getting more number of seconds then actual image pause. For suppose say, on the very first click of "Pass bag", image gets paused for 9.67 seconds, then after another image comes and for suppose say this image is paused for 6.75 seconds, so when I click on "pass bag" for this image I need to get 6.75. Instead I'm getting 9.67+6.75. Instead I need to record only 6.75. Appreciate any ideas on this..
Here are my functions for the "Pause bag" button Click and "Pass bag" Button Click:
function display_pause()
{
if (milisec_pause>=99)
{
milisec_pause=0;
seconds_pause+=1;
}
else
milisec_pause+=1;
dpause = seconds_pause + "." + milisec_pause;
timer_pause = setTimeout("display_pause()",10);
pause_Timer = dpause;
}
function btn_pause_click()
{
display_pause();
document.myForm.btn_pause.disabled = true;
document.myForm.btn1.disabled = false;
document.myForm.开发者_开发知识库btn_pass.disabled = false;
clearTimeout(Timer);
clearTimeout(msg_Timer);
}
function btn_pass_click()
{
alert(pause_Timer);
clearTimeout(pause_Timer);
pause_Timer = 0;
milisec_pause=0;
seconds_pause=0;
document.myForm.btn_pass.disabled = true;
document.myForm.btn1.disabled = false;
document.myForm.btn_pause.disabled = false;
Timer = setTimeout("slideit()",800);
if(flag == 1)
msg_Timer = setTimeout("msg(flag)",500);
}
There seems to be something wrong here:
timer_pause
is your timer:timer_pause = setTimeout("display_pause()",10);
pause_Timer
is the string with the current pause time in<sec>.<millisec>
dpause = seconds_pause + "." + milisec_pause; //... pause_Timer = dpause;
But
you're clearing
pause_Timer
and nottimer_pause
which is the var for the timerclearTimeout(pause_Timer);
and you're also setting it
pause_Timer
to0
pause_Timer = 0;
Not considering other issues (if any), you should at the least make this change:
clearTimeout(timer_pause);
Next, consider variable names that are more descriptive and less likely to be confused with each other!
Try setting pause_Timer=0; on pause button click before you call display_pause();
精彩评论