문제

I have

Myitem1.MyFunc();
Myitem2.MyFunc();

function MyFunc(){
  if (okToUpdate){
    //we weren't here from anywhere within the last 100 milliseconds
  }
}

Unfortunately, okToUpdate variable is updated not immediately, so it needs at least 100 milliseconds from the previous call of the function to have the correct value. How to make MyFunc be executed with the delay?

도움이 되었습니까?

해결책

setTimeout(function(){
       Myitem1.MyFunc();
},100)

will do the trick. If you explain what you want exactly I can give you exact timing code.

다른 팁

This code may help you

var clrTime=null;

MyFunc(){
    clrTime=setTimeout(function(){
        if (okToUpdate){
            // your code to do 
        }
    },100);
}

Using callbacks or promises would be the clean solution. Not some kind of busy waiting implemented with timeouts.

Have a look into publish/subscribe pattern.

I would use the setTimeout() function. Something like this:

<script>
function myFunction() {
    setTimeout(function(){MyFunc()},100);
}
</script>

<body onload="myFunction()">
</body>

setTimeout() method

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top