开发者

jQuery timeout change content

I have an issue. I use this code:

var test='test';
$('#tip').delay(1000).html(test);

That doesn't work, i开发者_如何转开发t won't change the text. Although, it works if I do html('test') instead of having a variable there. I've also tried using non jQuery JavaScript:

var test='test';
setTimeout("document.getElementById('tip').innerHTML=text", 1000);

As earlier, it only works if I directly input the text instead of a variable.

But how can I fix so it setTimeout works with variables too? Or is there any ind of work-around?

Btw, I use jQuery 1.6.3.

EDIT: Sorry, only the standard method (non jQuery) works as supposed when trying to do that without variable. With jQuery it makes it 'test' first, and the delay comes after.


I've just written a function:

(function(text){
    setTimeout(function(){
        document.getElementById("tip").innerHTML = text;
    }, 1000);
})(text)

The timeout is wrapped inside an anonymous function, so that multiple test variables can be used, and don't interfere with each other. Instead of wrapping the text in an anonymous function , you can also name the function, and call it from within a script: funcName("test text").

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜