Madhusudan
Madhusudan

Reputation: 4815

How to monitor window size changes using time delay in Java script?

I have a functionality in my javascript in which webpage content will get updated on resize of window. I want to monitor this using javascript automated window resize.

For this, I am trying something which looks like below:

<script>
  var myWindow = window.open("", "", "width=1000,height=1000");
  setTimeout("",5000);
  myWindow.resizeTo(700,700);
  setTimeout("",5000);
  myWindow.resizeTo(500,500);
  setTimeout("",5000);
</script>

But it's opening a window with first configuration (width = 500 and height = 500). It's not changing it's size after time delay interval.

I am not able to figure it out what is going wrong here? Can someone help me to figure it out what I am doing wrong here?

Upvotes: 0

Views: 60

Answers (1)

Pierre
Pierre

Reputation: 1343

Javascript setTimeout is asynchronous

console.log("a");
setTimeout(function(){
    console.log("b");
}, 5000);
console.log("c");

Will print:

a
c
b

It's not blocking like you could use a wait or a sleep in other languages.

So you have to do:

var myWindow = window.open("", "", "width=1000,height=1000");
setTimeout(function(){
    myWindow.resizeTo(700,700);
    setTimeout(function(){
        myWindow.resizeTo(500,500);
    }, 5000);
}, 5000);

Upvotes: 1

Related Questions