Javascript clearInterval在递归调用时不起作用

Javascript clearInterval在递归调用时不起作用,javascript,html,Javascript,Html,我在javaScript中有以下函数。当我检测到需要重新加载样式表时,将调用此函数。例如,doe将更改为用户语言,因此文本将不再适合按钮。问题是,它卡在setInterval部分。无休止地循环。我可以在chrome调试器中看到它确实到达了clearInterval部分,但它不会清除。此函数resetStyle只调用一次。 有人能帮忙吗? 谢谢大家! p.resetStyle = function () { var that = this; var oldStylesheet_hr

我在javaScript中有以下函数。当我检测到需要重新加载样式表时,将调用此函数。例如,doe将更改为用户语言,因此文本将不再适合按钮。问题是,它卡在setInterval部分。无休止地循环。我可以在chrome调试器中看到它确实到达了clearInterval部分,但它不会清除。此函数resetStyle只调用一次。 有人能帮忙吗? 谢谢大家!

p.resetStyle = function () {
    var that = this;
    var oldStylesheet_href = $('#mainStylesheet').attr("href");
    var i = oldStylesheet_href.indexOf('lang');
    var lastLanguege = oldStylesheet_href.substring(i + 5, i + 7);
    var prefix, sufix;

    if (lastLanguege === createjs.mainManager.LangString) {
        return;
    }

    prefix = oldStylesheet_href.substring(0, i - 1);
    sufix = '&' + oldStylesheet_href.substring(i + 7, oldStylesheet_href.length);


    var head = document.getElementsByTagName('head')[0]; // reference to document.head for appending/ removing link nodes
    var link = document.createElement('link'); // create the link node
    link.setAttribute('id', 'newStylesheet');
    link.setAttribute('href', prefix + '&lang=' + createjs.mainManager.LangString + sufix);
    link.setAttribute('rel', 'stylesheet');
    link.setAttribute('type', 'text/css');

    var sheet, cssRules;
    // get the correct properties to check for depending on the browser
    if ('sheet' in link) {
        sheet = 'sheet';
        cssRules = 'cssRules';
    } else {
        sheet = 'styleSheet';
        cssRules = 'rules';
    }

    var timeout_id = setInterval(function () { // start checking whether the style sheet has successfully loaded
        try {
            if (link[sheet] && link[sheet][cssRules].length) { // SUCCESS! our style sheet has loaded
                clearInterval(timeout_id); // clear the counters
                clearTimeout(timeout_id);
                that.onStyleReset();
            }
        } catch (e) {} finally {}
    }, 10), // how often to check if the stylesheet is loaded
        timeout_id = setTimeout(function () { // start counting down till fail
            clearInterval(timeout_id); // clear the counters
            clearTimeout(timeout_id);
            head.removeChild(link); // since the style sheet didn't load, remove the link node from the DOM
            that.onStyleReset();
        }, 15000);

    $('head').append(link);
    $('#mainStylesheet').remove();
    link.setAttribute('id', 'mainStylesheet');
};

您正在将变量
timeout\u id
用于两种不同的事情(您的interval id和timeout id),因此在调用
setTimeout
时会覆盖您的interval id。更改:

var timeout_id = setInterval(...
致:


并更新调用clearInterval的变量:
clearInterval(interval\u id)

您正在覆盖
timeout\u id
分配给interval的
timeout\u id
分配给timeout的值。也许您应该考虑
timeout\u id
的值:)考虑
var x=1,x=2中的
x
-这基本上就是你正在做的。谢谢!!天哪,真不敢相信我错过了!:
var interval_id = setInterval(...