我在 setTimeout 调用中做错了什么? [英] what am I doing wrong in my setTimeout call?

查看:52
本文介绍了我在 setTimeout 调用中做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些显示文本的代码,然后在两秒钟后将其删除,但是,setTimeout() 方法似乎对我来说工作不正常,被调用的函数正在无延迟地运行.代码如下:

I am trying to write some code that displays text, and then removes it two seconds later, however, the setTimeout() method doesn't seem to be working properly for me, the function that is called is running with no delay. Here's the code:

IDtext('IP-saved', "New IP Address saved successfully!");
setTimeout(IDtext('IP-saved', ""), 2000);

function IDtext(ID, text) {
    document.getElementById(ID).innerHTML = text;
}

我在这里做错了什么?

推荐答案

正确的语法是

setTimeout(IDtext, 2000, 'IP-saved', '');

被引用函数的参数必须放在最后

the arguments of the referenced function must be placed last

参见setTimeout()

你的语法方式是
var timeoutID = scope.setTimeout(function[, delay, arg1, arg2, ...]);

这篇关于我在 setTimeout 调用中做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆