Javascript 如何访问setInterval中的变量

Javascript 如何访问setInterval中的变量,javascript,ajax,jquery,popupwindow,Javascript,Ajax,Jquery,Popupwindow,我正在查看页面上显示一个弹出加载模式。。在给定时间后成功显示和隐藏 这是我的密码: function openSirenModal() { var timeout; $.modal({ contentAlign: 'center', width: 240, title: 'Loading', content: '<div style="line-height: 25px; padding: 0 0 10px"

我正在查看页面上显示一个弹出加载模式。。在给定时间后成功显示和隐藏

这是我的密码:

function openSirenModal() {
    var timeout;

    $.modal({
        contentAlign: 'center',
        width: 240,
        title: 'Loading',
        content: '<div style="line-height: 25px; padding: 0 0 10px"><span id="modal-status">Contacting to the device...</span><br><span id="modal-progress">0%</span></div>',
        buttons: {},
        scrolling: false,
        actions: {
            'Cancel': {
                color: 'red',
                click: function (win) {
                    win.closeModal();
                }
            }
        },
        onOpen: function () {
            // Progress bar
            var progress = $('#modal-progress').progress(100, {
                size: 200,
                style: 'large',
                barClasses: ['anthracite-gradient', 'glossy'],
                stripes: true,
                darkStripes: false,
                showValue: false
            }),

                // Loading state
                loaded = 0,

                // Window
                win = $(this),

                // Status text
                status = $('#modal-status'),

                // Function to simulate loading
                simulateLoading = function () {


                    var siren = "siren"
                    $.ajax({
                        type: "POST",
                        data: {
                            value: siren
                        },
                        url: "http://localhost/siren/siren/",

                        success: function (data) {

                            if (data == 1) {                                    progress.hideProgressStripes().changeProgressBarColor('green-gradient');
                                status.text('success!');
                                setTimeout(function () {
                                    win.closeModal();
                                }, 1500);

                            } else {
                                progress.hideProgressStripes().changeProgressBarColor('red-gradient');
                                setTimeout(function () {
                                    win.closeModal();
                                }, 1500);

                                status.text('error!');
                            }
                        },
                        error: function () {
                            alert("error");
                            progress.hideProgressStripes().changeProgressBarColor('red-gradient');
                            setTimeout(function () {
                                win.closeModal();
                            }, 1500);
                            status.text('error!');
                        }
                    });
                };

            // Start
            timeout = setTimeout(simulateLoading, 2500);
        },
        onClose: function () {
            // Stop simulated loading if needed
            clearTimeout(timeout);
        }
    });
};
这里我无法访问win变量,如何访问woin变量以便关闭弹出模式

最终代码

function openSirenModal() {
   var timeout, win;
var progress;
var status;

$.modal({
    contentAlign: 'center',
    width: 240,
    title: 'Loading',
    content: '<div style="line-height: 25px; padding: 0 0 10px"><span id="modal-status">Contacting to the device...</span><br><span id="modal-progress">0%</span></div>',
    buttons: {},
    scrolling: false,
    actions: {
        'Cancel': {
            color: 'red',
            click: function (win) {
                win.closeModal();
            }
        }
    },
    onOpen: function () {
        // Progress bar
        var progress = $('#modal-progress').progress(100, {
            size: 200,
            style: 'large',
            barClasses: ['anthracite-gradient', 'glossy'],
            stripes: true,
            darkStripes: false,
            showValue: false
        }),

            // Loading state
            loaded = 0,

            // Window
            win = $(this),

            // Status text
            status = $('#modal-status'),

            // Function to simulate loading
            simulateLoading = function () {




            };

        // Start
        //timeout = setTimeout(simulateLoading, 2500);
    },


    onClose: function () {
        // Stop simulated loading if needed
        clearTimeout(timeout);
    }


});

var siren = "siren";
$.ajax({
    type: "POST",
    data: {
        value: siren
    },
    url: "http://localhost/siren/siren/",

    success: function (data) {
        alert(data);
        if (data == 1) {

            var auto_refresh = setInterval(
                function () {

                    $.get('siren/sirenjson', function (datas) {

                        if (data == 1) {

                            $('#modal-progress').hideProgressStripes().changeProgressBarColor('green-gradient');
                            $('#modal-status').text('success!');
                           setTimeout(function (win) {
          win.closeModal();
           clearInterval(auto_refresh);
      }, 1500,win);


                        }
                    });

                }, 1000);

            //modl.onClose;
            //alert('hello');



         } else {
            progress.hideProgressStripes().changeProgressBarColor('red-gradient');
            setTimeout(function () {
                win.closeModal();
            }, 1500);


            status.text('error!');
        }

        //clearTimeout(timeout);
    },
    error: function () {

        alert("error");
        progress.hideProgressStripes().changeProgressBarColor('red-gradient');
        setTimeout(function () {
            win.closeModal();
          }, 1500);
        status.text('error!');
    }
      });

  };
函数openSirenModal(){ var超时,赢; var进展; var状态; 美元模态({ contentAlign:“中心”, 宽度:240, 标题:“加载”, 内容:“正在联系设备…
0%”, 按钮:{}, 滚动:false, 行动:{ “取消”:{ 颜色:“红色”, 点击:功能(win){ win.closeModal(); } } }, onOpen:函数(){ //进度条 变量进度=$(“#模式进度”)。进度(100{ 尺寸:200, 风格:'大', 棒类:[“无烟煤梯度”,“光滑”], 是的, 黑暗:错误, showValue:false }), //加载状态 已加载=0, //窗口 win=$(此), //状态文本 状态=$(“#模式状态”), //函数来模拟加载 SimulateLoad=函数(){ }; //开始 //超时=设置超时(模拟加载,2500); }, onClose:function(){ //如果需要,停止模拟加载 clearTimeout(超时); } }); var siren=“siren”; $.ajax({ 类型:“POST”, 数据:{ 价值:警笛 }, url:“http://localhost/siren/siren/", 成功:功能(数据){ 警报(数据); 如果(数据==1){ var auto_refresh=setInterval( 函数(){ $.get('siren/sirenjson',函数(数据){ 如果(数据==1){ $(“#模式进度”).hideProgressStripes().changeProgressBarColor('green-gradient'); $(“#模态状态”).text('success!'); 设置超时(功能(win){ win.closeModal(); clearInterval(自动刷新); },1500,赢); } }); }, 1000); //模型1.onClose; //警惕(“你好”); }否则{ progress.hideProgressStripes().changeProgressBarColor('red-gradient'); setTimeout(函数(){ win.closeModal(); }, 1500); status.text('error!'); } //clearTimeout(超时); }, 错误:函数(){ 警报(“错误”); progress.hideProgressStripes().changeProgressBarColor('red-gradient'); setTimeout(函数(){ win.closeModal(); }, 1500); status.text('error!'); } }); };
在毫秒参数后传递参数:

setTimeout(function (win) {
    win.closeModal();
    clearInterval(auto_refresh);
}, 1500,win);

但是,请注意,此方法需要使用polyfill进行修补,以便在小于10的Internet Explorer版本上使用。

首先关闭模式框,然后清除间隔

现在,在代码中,清除间隔首先存在

所以它停止,下一个代码没有执行,所以在这个顺序问题上

  clearInterval(auto_refresh);

                            * * win.closeModal(); * * //here i want to close the popup modal
变化

                                * * win.closeModal(); * * //here i want to close the popup modal
 clearInterval(auto_refresh);

难道你不能在
$(函数)
作用域中声明
win
变量,以便modal和ajax success都可以访问它吗?看看你提供的代码,看起来win从来没有用
var win=…
声明过,所以当你将win设置为一个值时,它被放在全局作用域上(这不是一个好主意)。您应该在某个地方声明它,以便可以访问它,但不能像kayen建议的那样在全局范围内。我将尝试提供一个例子,如果我可以复制和粘贴你的代码,并删除不相关的部分(90%)帮助我在这段代码..我只想关闭模式了。。。只要告诉我我应该在哪里声明winm,这样我就可以在代码中访问窗口并关闭它,前提是你不在任何地方声明它,但你可以在声明超时的地方声明它。@bryce提供给我的代码?我已经试过了。但是如果你想让它在IE中工作,它不会关闭modalOr;使用polyfil,将闭包传递给setTimeout或使用bind:但我认为这个范围问题当时没有解决,因为调用setTimeout时win不在范围内,所以kayen的注释可以解决这个问题。@BryceHanscomb我已经尝试过你的代码,但它不起作用。.弹出模式不起作用closing@hellosheikh大多数浏览器都有一个控制台,您可以按F12将其打开。控制台可能会向您显示一些错误,或者如果向代码中添加一些console.log语句,它可能会输出代码正在执行的操作。当你申请时,你可以提供更多有用的信息,而不是
它不起作用
,它将帮助你理解你的代码实际上在做什么。对于复杂的代码,console.log语句非常有用。请注意IE会在console.log上抛出一个错误,除非你打开调试窗口(F12)@HMR有一个错误出现在控制台上说win没有定义我不知道该告诉你什么-这是语法和功能上对我有效的代码。然后让win变量globaldid也为它。。。在全局定义之后,然后在运行代码时…无法调用未定义的方法“closeModal”“这个eror出现在我的控制台上,是jquery中的这个对话框ui@Riturajratanwin不必是全局的,您可以在超时后立即在
openSirenModal
函数中声明它,然后您可以通过函数参数(如var win call(win))发送win;函数调用(win){//在此函数中,您可以访问win变量};我只是给出一个想法,不是这样说的。。。希望这有意义
                                * * win.closeModal(); * * //here i want to close the popup modal
 clearInterval(auto_refresh);