c# - How to fix Javascript Timer In asp.net(Master Page concept) -
i trying add javascript timer in asp.net project, implemented , runing needed, if pressing key in disappering me. timer runing.
js. code
function mytimer(startval, interval, outputid, datafield) { this.value = startval; this.outputcntrl = document.getelementbyid(outputid); this.currenttimeout = null; this.interval = interval; this.stopped = false; this.data = null; var formels = document.documentelement; if (datafield) { (var = 0; < formels.length - 1; i++) { if (formels[i].name == datafield) { this.data = formels[i]; = formels.length + 1; } } } mytimer.prototype.go = function () { if (this.value > 0 && this.stopped == false) { this.value = (this.value - this.interval); if (this.data) { this.data.value = this.value; } var current = this.value; this.outputcntrl.innerhtml = this.hours(current) + ':' + this.minutes(current) + ':' + this.seconds(current); this.currenttimeout = settimeout("timer.go()", this.interval); } else { alert('time out!'); //window.location('index.aspx'); } } mytimer.prototype.stop = function () { this.stopped = true; if (this.currenttimeout != null) { cleartimeout(this.currenttimeout); } } mytimer.prototype.hours = function (value) { return math.floor(value / 3600000); } mytimer.prototype.minutes = function (value) { return math.floor((value - (this.hours(value) * 3600000)) / 60000); } mytimer.prototype.seconds = function (value) { var hoursmillsecs = (this.hours(value) * 3600000) var minutesmillsecs = (this.minutes(value) * 60000) var total = (hoursmillsecs + minutesmillsecs) var ans = math.floor(((this.value - total) % 60000) / 1000); if (ans < 10) return "0" + ans; return ans; } }
i'm calling button control.
code
void page_preinit(object sender, eventargs e) { string timerval = request.form["timerdata"]; if (timerval != null || timerval == "") { timerval = timerval.replace(",", string.empty); timerstartvalue = long.parse(timerval); } } private int32 timerinterval { { object o = viewstate["timerinterval"]; if (o != null) { return int32.parse(o.tostring()); } return 50; } set { viewstate["timerinterval"] = value; } } protected void button1_click(object sender, eventargs e) { stringbuilder bldr = new stringbuilder(); bldr.appendformat("var timer = new mytimer({0},{1},'{2}','timerdata');", this.timerstartvalue, this.timerinterval, this.lbltimercount.clientid); bldr.append("timer.go()"); clientscript.registerstartupscript(this.gettype(), "timerscript", bldr.tostring(), true); clientscript.registerhiddenfield("timerdata", timerstartvalue.tostring()); } void page_prerender(object sender, eventargs e) { stringbuilder bldr = new stringbuilder(); bldr.appendformat("var timer = new mytimer({0},{1},'{2}','timerdata');", this.timerstartvalue, this.timerinterval, this.lbltimercount.clientid); bldr.append("timer.go()"); clientscript.registerstartupscript(this.gettype(), "timerscript", bldr.tostring(), true); clientscript.registerhiddenfield("timerdata", timerstartvalue.tostring()); }
i'm displaying label control.
<asp:label id="lbltimercount" runat="server" height="27px" width="232px"></asp:label>
in page_preinit 'string timerval' value coming same after page_load i'm passing, starting timer starting.?
please correct me.
these simple steps need follow: 1) upon page load, set timerinterval on hidden element
your html should this:
<input type="button" id="btnstarttimer" name="starttimer" value="start" /> <input type="hidden" id="hdninterval" name="interval" value="50" /> <p> <label>show timer:</label> <label id="lbloutput"></label> </p> $('#yourbuttonid').on('click', function(e){e.preventdefault; timer.go(); });
your button js click event should this:
$('#btnstarttimer').on("click", function (e) { e.preventdefault(); var interval = $('#hdninterval').val(); var timer = new mytimer(0, interfal, "lbloutput", null); timer.go(); });
something along lines. make post backs unnecessary
Comments
Post a Comment