简体   繁体   中英

why does my JS code runs only once?

Here's my question:

I have an script tag at the end of my document, with this:

<script>
    $("#one").css("height",$("#two").width()/2);
</script>

And I want it to keep refreshing the code... But it only runs when I refresh the page. What should I do to keep running it?

<script>
    setInterval(function() {
        $("#one").css("height",$("#two").width()/2);
    }, 1000);
</script>

This will repeat the function every 1000ms (=1s).

If this is to animate something you can use the jquery .animate

$("#one").animate({height:0},1000);

this animates the height down to 0 and doing so over 1000 ms. there is plenty of other options with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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