简体   繁体   English

无法在特定点停止进度栏

[英]Not Able to Stop Progress Bar on Certain Point

Demo 演示

Using jQuery progressbar.js plugin I am not able to stop the progress bar at any certain point. 使用jQuery progressbar.js插件,我无法在任何时候停止进度栏。 Unfortunately the plugin api didn't explain how to do this but I tried to do it on my own by declaring a target variable which is storing progress value. 不幸的是,插件api并未说明如何执行此操作,但我尝试通过声明存储进度值的目标变量来自己执行此操作。

 target = bar.value();

Then I tried to stop the progress by applying an if condition like this 然后我尝试通过应用这样的if条件来停止进度

if ((target * 100).toFixed(0) == 60) {
    circle.stop();
}

but this is not stopping the progress bar on 60! 但这并不能阻止进度条在60上! I tried to use this based on the circle.stop(); 我试图基于circle.stop();使用它circle.stop(); function (If you click on stop button it stops the progess and alerts the value of bar (target) but the if stattement not working! 功能(如果单击停止按钮,它将停止进度并警告bar(目标)的值,但是if语句不起作用!

var target;
var circle = new ProgressBar.Circle('#container', {
    color: '#FCB03C',
    strokeWidth: 3,
    trailWidth: 1,
    duration: 1500,
    bar: 60,
    text: {
        value: '0'
    },
    step: function (state, bar) {
        bar.setText((bar.value() * 100).toFixed(0));
        target = bar.value();
    }
});

circle.animate(1, function () {
    circle.animate();
})


if ((target * 100).toFixed(0) == 60) {
    circle.stop();
}


$('#stop').on('click', function () {
    circle.stop();
    alert((target * 100).toFixed(0));
});

Can you please let me know what I am doing wrong and how I can solve this? 您能否让我知道我做错了什么以及如何解决这个问题? Thanks 谢谢

The reason your demo isn't working is you're checking the target only once, when it's just declared, without any value. 演示不起作用的原因是,仅在声明目标时检查一次目标,而没有任何值。 (undefined * 100).toFixed(0) == "NaN" . (undefined * 100).toFixed(0) == "NaN" Since javascript isn't reactive by default, you have to check the condition every time target updates. 由于javascript默认情况下是不响应的,因此每次target更新时都必须检查条件。 The step function is perfect for this, since it's called every time value changes. step函数非常适合此操作,因为每次值更改时都会调用该函数。 To fix the error, move: 要解决该错误,请移动:

if ((target * 100).toFixed(0) == 60) {
    circle.stop();
}

inside step function: 内部step功能:

step: function (state, bar) {
    bar.setText((bar.value() * 100).toFixed(0));
    target = bar.value();
    if ((target * 100).toFixed(0) == 60) {
        circle.stop();
    }
}

Fixed demo . 固定演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM