0

I have the following code,

function myCustomFunction() {
    setTimeout(function () {
        $('#autoplay').get(0).click();
    }, 2000);
}

var isAutoplay = localStorage.getItem('autoplay');

if (isAutoplay) {
    myCustomFunction();
}

$('#autoplay').on('click', function () {
    localStorage.setItem('autoplay', true);
    myCustomFunction();
});

// CLEAR THE LOCALSTORAGE
$('#stopplay').on('click', function () {
    localStorage.removeItem('autoplay');
}); 

I assumed by clearing "localStorage" it would register "isAutoplay" to be false and in turn stop the myCustomFunction() function, but instead "localStorage.removeItem('autoplay');" doesn't seem to be clearing it.

How else can I stop the function from working by clicking on a link to stop the autoplay?

Thank you

4

2 回答 2

0

我不确定您的目标是什么,因为您的代码没有任何用处。但也许你想要这个:

var timer;
function myCustomFunction() {
    timer = setTimeout(function () {
        $('#autoplay').get(0).click();
    }, 2000);
}

var isAutoplay = localStorage.getItem('autoplay');

if (isAutoplay) {
    myCustomFunction();
}

$('#autoplay').on('click', function () {
    localStorage.setItem('autoplay', true);
    myCustomFunction();
});

// CLEAR THE LOCALSTORAGE and remove the timer
$('#stopplay').on('click', function () {
    localStorage.removeItem('autoplay');
    clearTimeout(timer);
}); 
于 2013-06-26T19:11:27.833 回答
0

你的计时器总是模拟一个click()调用$('#autoplay').get(0)哪个localStorage.setItem('autoplay', true);

你在一个循环中。

要解决它,您必须将这两个操作分开,或者单击时手动取消计时器$('#stopplay')

更多解释将帮助我为您生成正确的代码。

于 2013-06-26T19:18:00.777 回答