VUE do an ajax poll. Where should the polling function be written?

now I have a requirement that I start to make a polling request all the time when I need to enter a page
I want to ask me where this polling request should be written
for example, should my polling function be fn () {}
, should it be written in created {}

?
May.11,2021

write the method normally, and then create a setInterval in create to call this method to save the return value of setInterval. Remember to clean up this timer inside the unmounted lifecycle function, otherwise you will report an error


created() {
  window.InitSetInterval = setInterval(fn(),2000)
},
mounted() {
},
destroyed: {
   clearInterval(window.InitSetInterval )
}


is it better to use settimeout, because you should poll every time you enter this page, and you should only do

once?
Menu