请教JS中setTimeout中函数的写法

请教JS中setTimeout中函数的写法,第1张

如果在settimeout里面使用this,最终到执行时,this代表window,因为这是延时执行,执行的对象this是setTimeout,所以折衷的办法是提前获得对象并赋给一个名称,然后再使用。

<input id=ss name="S_T" type="text" onkeyup="windowS_T=this; setTimeout('finit(S_T)', 500);" />

1)局部变量里的新变量,应加上var,防止覆盖修改全局变量,

2)javascript是区分大小写的,原函数是setTimeout

3)setTimeout第一个参数应该用函数、函数名,避免使用代码字符串,比如你传入的语句,dd将会未定义,执行你传入的代码字符串环境,其实无法读到你当前环境的变量。

/  说明,delaySet(元素,属性名,起始值,间隔,最终值)  元素可以传入 #+id,$+name,+class,如果没有# $ 将会当成标签tagName,也可以传入你自己找到的元素或元素集合 /

function delaySet(el, name, direct, timeout, instead) {

    function trans(el, c) {

        c = elcharAt(0);

        c = cmatch(/^[#\\$]/)  c : "";

        return typeof el === "object" && ObjectprototypetoStringcall(el)match(/\[object HTMLElement\]/)  el : typeof el === "string" && (el = (document[["getElementById", "getElementsByClassName", "getElementsByName", "getElementsByTagName"][{

            "#" : 0,

            "" : 1,

            "$" : 2,

            "" : 3

        }

                [c]]])(elsubstring(c  1 : 0)), c  (el = [el]) : ellength)  el : null;

    }

 function attr(el, name, val) {

        el = trans(el);

        if (el && typeof name === "string") {

            if (argumentslength >= 3) {

                if (ellength) {

                    var l = ellength;

                    while (l--) {

                        attr(el[l], name, val);

                    }

                    return;

                }

                if (elsetAttribute) {

                    elsetAttribute(name, val);

                } else {

                    el[name] = val;

                }

            } else {

                el = ellength && el[0] || el;

                if (elgetAttribute) {

                    return elgetAttribute(name);

                }

                return el[name];

            }

        }

    }

    if (trans(el)) {

        attr(el, name, direct);

        setTimeout(function () {

            attr(el, name, instead);

        }, timeout);

    }

}

function mao(el, name, timeout) {

    function direct(name) {

        return "img/" + name + "gif";

    }

    function instead(name) {

        return "img/zhong" + name + "png"; //这里地址似乎应该是"img/zhong/"

    }

    delaySet(el, "src", direct(name), parseInt(timeout) || 500, instead(name));

}

mao("#shiyan", "05", 500);

我给你个函数吧。。带参数传递的sttimeout

function JTimeout(funcName, time) {

var args = [];

for (var i = 2; i < argumentslength; i++) {

argspush(arguments[i]);

}

return windowsetTimeout(function() {

funcNameapply(this, args);

}, time);

}

使用方法:

JTimeout(show_date_time, 1000, new Date(eval("new " + eval(currTime)source)), new Date(eval("new " + eval(sysTime)source)));

第一个参数为你要运行的函数,第二个参数为延迟,后面可以跟任意多个参数

setTimeout(

    function(self)

    {

      return function()

      {

        alert(self);//这里面用self代替this

      }

    }(this), 1000);

你试试看

大哥们你们不知道有闭包这东西吗function 内是可以有function的

function docomment_form_hidden(doid, id) {

var showid = 'docomment_form_'+doid+'_'+id;

var hiddenid = 'docomment_form_show_'+doid+'_0';

var focusid = 'do_message_'+doid+'_'+id;

var append_parent = 'append_parent';

function timeoutshow(){

$(showid)styledisplay = 'none';

$(hiddenid)styledisplay = '';

}

setTimeout("timeoutshow();",2000)

$(append_parent)innerHTML = '';

}

在这里你运行不了是因为setTimeout("timeoutshow();",2000)这句话的根对象是window,编译器会自动去全局变量里查timeoutshow()函数,当然是没有的,正确写法是setTimeout(timeoutshow,2000)

是什么问题呢?

setTimerout的第一个参数如果是字符串的话,请写成

setTimeout("hello()", 200),括号是不能少的,不然他不会执行的

第二,你的hello方法中还要再次调用setTimerout("", 200);

因为setTimerout只会运行一次,你要想多次运行,请使用setInterval方法,参数与setTimerout一样

首先,setTimeout是可以取消的,可以不让其永远不终止,方法如下:

var timer = null;

function f(){

    alert("k");

    timer = setTimeout(f,1000);

}

// 在另外一个函数中,在某种条件下,取消之前的定时器

clearTimeout(timer);

这样,timer就不再执行了,也就不会再次调用函数f,生成新的定时器。

其次,关于内存,js类似于java有垃圾回收机制,一些不会再次用到的变量等,会被清理。只要是作用域不逃出函数f的,函数f执行完毕后,js总是有办法将其销毁,当然不一定百分百销毁。这样的话,内存怎么会越来越多呢?只有函数f在执行的时候会多一些,但随即又会被清理的。

最后,即便没有clearTimeout,在浏览器关闭的时候,一切都会消失的。

要提醒的是,如果函数f里在不停的创建对象,而且在函数f执行完毕后,这些对象又是被引用的,确实会有内存被越来越多的占用。

你可以将函数先赋给一个变量

var handler = function(){

//你的代码

setTimeout(handler,20000);//设置20秒调用一次

};

windowonload = handler;

我没看明白你的意思,鼠标移过显示分数为什么要设置20秒调用一次

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/13494631.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-09-01
下一篇2025-09-01

发表评论

登录后才能评论

评论列表(0条)

    保存