Home > front end >  The elder for help
The elder for help

Time:10-05

Due to study dynamic website, this is our learning materials on the code, but after the refresh the page, the countdown will restart it, didn't speak this knowledge, the teacher who can help to add the code inside, let it not reset, I study the code know,
(function ($) {
$. Fn. Timeout=function (options) {
The options=$. The extend ({
"MaxTime" : 60,//time (seconds)
"OnTimeOver" : the function () {}//it is time to perform the function of
}, options);
Var $thisObj=this;
Var maxTime=options. MaxTime;//the countdown (seconds)
Var timer=setInterval (update, 1000);//1 seconds to update a
update();//automatic updates for the first time
//automatic update
The function update () {
If (maxTime>=0) {
Var minutes=math.h floor (maxTime/60);//calculate the minutes
Var seconds=math.h floor (maxTime % 60);//calculate number of seconds
$thisObj. Text (the fill (minutes) + ":" + the fill (seconds));
- maxTime;
} else {
ClearInterval (timer);
Options. OnTimeOver ();//function call
}
}
//less than 10 automatically fill 0
The function the fill (s) {
The return of s & lt; 10? '0' + s: s;
}
};
}) (jQuery);

CodePudding user response:

This should be you encapsulate a method, using the need to pass a parameter options object, inside maxTime represents the start value, the default is 60,
Js is a scripting language, each time you refresh the page will perform again and again so now when you refresh the page will start again, want to keep state there are two ways,
One, keep the current state in local, that is, every time the countdown, the current value stored in localStorage (maxTime a change, can save a), and then enter the page from
In localStorage for you to save the value of the, if there is preserved value, put the value as a parameter passed to this method, you if not, just use your default values,
This method need to note that localStorage will not automatically delete records, need you to delete (such as maxTimeTwo, every time is entering a page from the back end to obtain the initial value, and then to get to the initial value passed in line directly, as for the other to do is to the backend

CodePudding user response:

Can be achieved by the new date, set up a first, and then you want in a new time, both is presupposed, is the countdown, you want and then carry digital subtraction, on the page,

CodePudding user response:

Need a place to save your value, you can use window. The localStorage, can also use cookies
  • Related