動態顯示時分秒

前端 

var allTime = Math.round(data.RemainTime)

var Hours = Math.floor(allTime / (60 * 60));
var allMunite = Math.floor(allTime % (60 * 60));
var munite = Math.floor(allMunite / 60);
var second = Math.floor((allMunite % 60));
var currentTime = (Hours < 10 ? '0' + Hours : Hours) + ':' + (munite < 10 ? '0' + munite : munite) + ':' + (second < 10 ? '0' + second : second);
$('.spanTimeshow').html(currentTime)

refreshtime($('.spanTimeshow'), data.RemainTime)

    function refreshtime(obj, spantime) {
            //clearInterval(setInt);
      var setInt=setInterval(function () {
                //播放時間、總時間
                var allTime1 = Math.round(spantime)
                var Hours1 = Math.floor(allTime1 / (60 * 60));
                var allMunite1 = Math.floor(allTime1 % (60 * 60));
                var munite1 = Math.floor(allMunite1 / 60);
                var second1 = Math.floor((allMunite1 % 60));
                var currentTime1 = (Hours1 < 10 ? '0' + Hours1 : Hours1) + ':' + (munite1 < 10 ? '0' + munite1 : munite1) + ':' + (second1 < 10 ? '0' + second1 : second1);
                $(obj).html(currentTime1)
                if (spantime<=0)
                {
                    $('.groupsh').addClass('hide');
                    clearInterval(setInt);
                }
                spantime--;
            }, 1000)


    }



    後端

        public void GetRroupList(HttpContext context)
        {
            string json="";
            int productId = Globals.RequestFormNum("productId");
            DataTable dt=OrderHelper.GetGroupList(productId);
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                DateTime dtnow = Convert.ToDateTime(DateTime.Now.ToLocalTime());
                DateTime dtend = Convert.ToDateTime(dt.Rows[i]["GroupEDate"]);
                TimeSpan ts = dtend.Subtract(dtnow);    //時間差
                json += "{\"CategoryId\":\"" + dt.Rows[i]["CategoryId"] + "\",\"GroupUser\":\"" + dt.Rows[i]["GroupUser"] + "\",\"RemainTime\":\"" + ts.TotalSeconds + "\",\"sy\":\"" + dt.Rows[i]["sy"] + "\"},";
            }
            if (!string.IsNullOrEmpty(json))
            {
                json = json.Substring(0, json.Length - 1);                
            }
            json = "[" + json + "]";
            context.Response.Write(json);
        }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章