setTimeout延迟一个动作

时间:2013-07-28 19:40:46

标签: javascript jquery

我想在单击更新按钮时显示一个窗口,此窗口应保持3秒,然后关闭后自动结束执行更新操作。这是我的代码,但它不起作用

my $cScript = qq{

\$(document).ready(function(){
    var w;
    function closeWindow(){ 
        setTimeout(function() {
           w.close();
        }, 3000);
    }

    function createWindow(){
        //create the popup window.

        w=window.open("","",'width=200,height=100');

        // put something into the popup window
        try{
            w.document.write('<html><head></head><body><p>Updating...</p></body> <html>')
        }catch(err){
            //handle error here
        }
        closeWindow();

    });
};

print $q->script($cScript);
 }

HTML表单:

 $cInput_form .= $q->image_button({
      -src =>  '/media/images/save_1.png',
      -class=>'btn btn-primary btn-large',
      -title => 'update', 
      -name => 'Update', 
      -value => $row_id,
      -onclick => "createWindow()"
});



print $q->fieldset ({-class => "ui-widget ui-widget-content"}, $cInput_form);

问题出在哪里?

1 个答案:

答案 0 :(得分:2)

DEMO: jsFiddle

<强> JS:

var w;

createWindow();

function closeWindow() {
    setTimeout(function () {
        w.close();
    }, 3000);
}

function createWindow() {
    //create the popup window.
    var htmlText = "<p>Updating...</p>";
    w = window.open("", "", 'width=200,height=100');
    $(w.document.body).html(htmlText);
    closeWindow();
};