/    Sign up×
Community /Pin to ProfileBookmark

Stop-clear interval

Hi,
I have checkBox in my code which allow me to set interval.
When checkBox is checked, this funcion works well but don’t know why won’t stop when checkBox is unchecked?
If i add alert(“uncecked”) in else block and uncheck listBox,alert popup.So something is about Interval.clearInterval();
Thnaks for any help.

[code]
$(function()
{

$(‘[name=”updataCheckBox”]’).change(function()
{

if ($(this).is(‘:checked’)) {

var Interval=setInterval(function(){

$(“#updateButton”).click();

},2000);

}
else
{
Interval.clearInterval();
}
});
});
[code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMar 08.2020 — There are two issues in your code:
  • 1. The variable Interval is defined inside the scope of the anonymous function of the change handler. Therefore it is not available when the button is unchecked. It has to be defined outside of that function.

  • 2. The variable Interval has to be handed over to the function clearInterval() as a parameter.
    $(function () {
    var Interval;
    $('[name="updataCheckBox"]').change(function () {
    if ($(this).is(':checked')) {
    Interval = setInterval(function () {
    $("#updateButton").click();
    }, 2000);
    }
    else {
    clearInterval(Interval);
    }
    });
    });
  • Copy linkTweet thisAlerts:
    @AhmoauthorMar 08.2020 — I see.That have sense.Thank you for your time and effort
    ×

    Success!

    Help @Ahmo spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.25,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...