简体   繁体   English

setTimeout()不执行

[英]setTimeout() not executing

I've got this simple function which displays a message to the user. 我有这个简单的功能,可以向用户显示一条消息。 If I add the timeout parameter it will slide back up automatically if not the user has to click it to get rid. 如果我添加了timeout参数,它将自动向后滑动,如果不需要,用户必须单击它才能摆脱。 But the timeout bit isn't working. 但是超时位不起作用。

function feedback(text, timeout){
    $('#feedback').text(text).slideDown('fast');

    $('#feedback').click(function(){
        $(this).slideUp();
    });
    if(timeout){
        setTimeout(function(){
            $('#feedback').slideup();
        }, timeout);
    }
}

The problem is that $('#feedback').slideup(); 问题是$('#feedback').slideup(); needs a capital U in there (eg .slideUp() ). 在其中需要一个大写.slideUp() U (例如.slideUp() )。 You can also shorten it down a bit overall doing this: 您还可以总体上将其缩短一点:

function feedback(text, timeout){
    var fb = $('#feedback').text(text).slideDown('fast');
    if(timeout) fb.delay(timeout).slideUp();

    fb.click(function(){
        $(this).slideUp();
    });
}

This uses the built-in delay() functionality of jQuery to achieve the same effect in a more concise way. 这使用jQuery的内置delay()功能以更简洁的方式实现相同的效果。

putting a whole function call inside a set timeout function is not valid. 将整个函数调用放入设置的超时函数中无效。 Your setTimeout will never trigger as you might want it to, as setTimeout will only accept a function reference. 您的setTimeout将永远不会触发,因为setTimeout将仅接受函数引用。

setTimeout(function(){
  $('#feedback').slideup();
}, timeout);

Try the following code: 尝试以下代码:

var ref = function() {
  $('#feedback').slideup();
};
setTimeout(ref, timeout);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM