javascript - How to create a loop and stop it if condition is met? -


i have function posts message parent component after 1 second.

settimeout(function(){   var widgetheight =  getdocheight();  parent.postmessage(widgetheight, hosturl); }, 1000) 

i want refactor create loop checks widgetheight every 1/10th second , if changed post message parent , stop loop.

how can this?

use interval create repeatable timer.

        let timer         let prevwidgetheight = getdocheight(); //get initial height         timer = setinterval(function(){            let widgetheight =  getdocheight(); //get current height           //compare new height previous height:           if(widgetheight !== prevwidgetheight) { //if current height not same initial height,             prevwidgetheight = widgetheight              parent.postmessage(widgetheight, hosturl); //make update             clearinterval(timer) //stop timer           }               }, 100) //100ms 1/10th of second 

this stops loop once height differs height set before starting loop. stopping means new changes won't passed on though.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -