user3070003
user3070003

Reputation:

Will javascript loop make my page get eventually stuck?

i have this function:

<script language="javascript">
function live(){
    var d = $live;
    var elm = document.getElementById("live");
    if(d==1){
        elm.style.display = 'block';
    } else{
        elm.style.display = 'none';
    }     
}
</script>

setInterval(function(){live();},10000);

and im just concerned about my page getting stuck after having it open on the browser for a while or causing my users browser to stop responding or anything like that. How safe is to use loops like this?

Is this what google or facebook use to show new notifications alerts on their page in real time? That seems to go pretty smoothly.

Thank you.

Upvotes: 0

Views: 100

Answers (2)

adhodi
adhodi

Reputation: 26

Use of setInterval is a common practice for showing notifications on websites. It wont hang your page, although you must clear the interval once it is no longer required. Say you have already shown the notification, so better hold the reference of setInterval so that you could clear it later.

var ref = setInterval(fn, 100);
clearInterval(ref);

Upvotes: 0

ThoolooExpress
ThoolooExpress

Reputation: 33

This isn't a loop in the traditional sense, it's really just a function which is called at a regular interval, so you are in the clear here. Just be careful that nothing increases the memory use each time it executes, as that is what will most likely be what will kill the user's browser.

Also, the setInterval needs to me in a script tag, otherwise it will show up on your page.

Upvotes: 1

Related Questions