رفتن به مطلب

قرار دادن تابع بازگشتی(recursive)در جاوااسکریپت


mk7887

پست های پیشنهاد شده

سلام خسته نباشید دوستان من داخل سایتم یک شمارش معکوس دارم که داخل یک اسلایدر قرار داره و تکرار میشه وکدش را پایین گذاشتم حالا میخوام زمانی شمارش معکوس اون تموم شد وبه صفر رسیددوباره برگرده واز اول همون مقداری رو که براش تعیین کرده بودم رو شمارش کنه.

این کد رو از سایتcodpenبرداشتم ولی نتوانتستم موفق بشم و این تابع باز گشتی را پیاده کنم فکر کنم باید این قسمت[if (0 === t.length) return this.clearTimer(e), e.trigger("complete"), !1;] این کد رو تغییر داد اگر ممکن است کمکم کنید.اینم ادرس سورس کد شمارش معکوس در سایتcodepen که در پایین گذاشتم:https://codepen.io/piyush-tapaniya/pen/xooopa.

 

<div class="container">
  <h1> <span class="timer" data-minutes-left="1"></span></h1>
</div>

<style>
span.timer {
    display: inline-flex;
}
 span.timer {
    font-size: 15px; 
    margin-right:140px;    
}
div.c-product-box__amazing{
  
  display: flex;
  flex-direction: column;
  justify-content: center;
  
  height:20px;

}
</style>
<script>
! function (t) {
    "object" == typeof module && "object" == typeof module.exports ? module.exports = function (e) {
        t(e, window, document)
    } : t(jQuery, window, document)
}(function (t, e, n, o) {
    var s;
    Number.isFinite = Number.isFinite || function (t) {
        return "number" == typeof t && isFinite(t)
    };
    var i = function (t) {
        return this._options = {}, this.targetElement = t, this
    };
    i.start = function (t, e) {
        return function (t, e) {
            e = e || {}, t._options.elementContainer = e.elementContainer || "div";
            var n = e.classNames || {};
            t._options.classNameSeconds = n.seconds || "jst-seconds", t._options.classNameMinutes = n.minutes || "jst-minutes", t._options.classNameHours = n.hours || "jst-hours", t._options.classNameClearDiv = n.clearDiv || "jst-clearDiv", t._options.classNameTimeout = n.timeout || "jst-timeout"
        }(s = new i(e), t), s.start(t)
    }, i.prototype.start = function (e) {
        var o = this,
            i = function (t) {
                var e = n.createElement(o._options.elementContainer);
                e.className = o._options.classNameSeconds;
                var s = n.createElement(o._options.elementContainer);
                s.className = o._options.classNameMinutes;
                var i = n.createElement(o._options.elementContainer);
                i.className = o._options.classNameHours;
                var r = n.createElement(o._options.elementContainer);
                return r.className = o._options.classNameClearDiv, t.append(i).append(s).append(e).append(r)
            };
        this.targetElement.each(function (n, o) {
            var r = this,
                a = t(o),
                u = a.attr("class");
            return a.on("complete", function () {
                clearInterval(a.intervalId)
            }), a.on("complete", function () {
                a.onComplete(a)
            }), a.on("complete", function () {
                a.addClass(r._options.classNameTimeout)
            }), a.on("complete", function () {
                e && !0 === e.loop && s.resetTimer(a, e, u)
            }), a.on("pause", function () {
                clearInterval(a.intervalId), a.paused = !0
            }), a.on("resume", function () {
                a.paused = !1, r.startCountdown(a, {
                    secondsLeft: a.data("timeLeft")
                })
            }), i(a), this.startCountdown(a, e)
        }.bind(this))
    }, i.prototype.resetTimer = function (t, e, n) {
        var o = 0;
        e.loopInterval && (o = 1e3 * parseInt(e.loopInterval, 10)), setTimeout(function () {
            t.trigger("reset"), t.attr("class", n + " loop"), s.startCountdown(t, e)
        }, o)
    }, i.prototype.fetchSecondsLeft = function (t) {
        var e = t.data("seconds-left"),
            n = t.data("minutes-left");
        if (Number.isFinite(e)) return parseInt(e, 10);
        if (Number.isFinite(n)) return 60 * parseFloat(n);
        throw "Missing time data"
    }, i.prototype.startCountdown = function (t, e) {
        e = e || {};
        var n = null,
            o = function () {
                return clearInterval(n), this.clearTimer(t)
            }.bind(this);
        t.onComplete = e.onComplete || o, t.allowPause = e.allowPause || !1, t.allowPause && t.on("click", function () {
            t.paused ? t.trigger("resume") : t.trigger("pause")
        });
        var s = e.secondsLeft || this.fetchSecondsLeft(t),
            i = e.refreshRate || 1e3,
            r = s + this.currentTime(),
            a = r - this.currentTime();
        this.setFinalValue(this.formatTimeLeft(a), t), n = setInterval(function () {
            (a = r - this.currentTime()) < 0 && (a = 0), t.data("timeLeft", a), this.setFinalValue(this.formatTimeLeft(a), t)
        }.bind(this), i), t.intervalId = n
    }, i.prototype.clearTimer = function (t) {
        t.find(".jst-seconds").text("00"), t.find(".jst-minutes").text("00:"), t.find(".jst-hours").text("00:")
    }, i.prototype.currentTime = function () {
        return Math.round((new Date).getTime() / 1e3)
    }, i.prototype.formatTimeLeft = function (t) {
        var e = function (t, e) {
                return (t += "").length >= (e = e || 2) ? t : Array(e - t.length + 1).join(0) + t
            },
            n = Math.floor(t / 3600);
        t -= 3600 * n;
        var o = Math.floor(t / 60);
        t -= 60 * o;
        var s = parseInt(t % 60, 10);
        return 0 == +n && 0 == +o && 0 == +s ? [] : [e(n), e(o), e(s)]
    }, i.prototype.setFinalValue = function (t, e) {
        if (0 === t.length) return this.clearTimer(e), e.trigger("complete"), !1;
        e.find("." + this._options.classNameHours).text(t.pop() + ":"), e.find("." + this._options.classNameMinutes).text(t.pop() + ":"), e.find("." + this._options.classNameSeconds).text(t.pop())
    }, t.fn.startTimer = function (t) {
        return this.TimerObject = i, i.start(t, this), this
    }
});


$(function () {
    $('.timer').startTimer();
})
</script>  

لینک به ارسال
  • yazdaniwp این مورد را قفل کرد موضوع

سلام. لطفا برای هر موضع فقط یک مرتبه تاپیک ایجاد کنید. تعداد تاپیک مشابه کمکی به اینکه زودتر پاسخ دریافت کنید نمیکنه و باعث میشه بخاطر عدم رعایت قوانین انجمن، جریمه بشید. لطفا رعایت کنید.

پاسختون رو در تاپیک دیگه ای دادم.

  • پسندیدن 1
لینک به ارسال
مهمان
این موضوع برای عدم ارسال قفل گردیده است.
×
×
  • اضافه کردن...