开发者

jQuery timeout change content

开发者 https://www.devze.com 2023-04-09 15:34 出处:网络
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\') in

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

精彩评论

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