setTimeout改进 每间隔0.1秒移动一次改进

var message = {
      settings:{
        x_max:200,
        y_max:100,
        times:100
      },
      postitionMessage:function(){
      this.parap = document.getElementById("message");
      this.parap.style.cssText = "position:absolute;left:100px;top:50px;";
   return this.movieMessage();
     },
     movieMessage:function(){
     var xpos = parseInt(this.parap.style.left);
     var ypos = parseInt(this.parap.style.top);
     if(xpos<this.settings.x_max){xpos+=1;};
     if(ypos<this.settings.y_max){ypos+=1;};
     this.parap.style.cssText = "position:absolute;left:"+xpos+"px;top:"+ypos+"px;";
  if(xpos<this.settings.x_max || ypos<this.settings.x_max){
     setTimeout(function(){message.movieMessage()},message.settings.times);
  }
    }
 };
function messageObject(){return message.postitionMessage()};
window.onload = messageObject;
在Vue.js项目中,你可以使用定时器来实现每隔5执行一次的功能。然而,需要注意的是,单纯使用setInterval会导致页面卡死的问题。因为setInterval不会清除定时器队列,每次执行都会导致定时器叠加,最终导致页面卡死。为了避免这个问题,你可以使用setTimeout来清除定时器。以下是一个正确的解决方法: ```javascript let timer = null; function executeEvery5Seconds() { // 执行你的代码逻辑 // ... // 设置下一次执行 timer = setTimeout(executeEvery5Seconds, 5000); } // 在Vue组件中调用定时器 mounted() { timer = setTimeout(executeEvery5Seconds, 5000); }, // 在Vue组件销毁时清除定时器 beforeDestroy() { clearTimeout(timer); } ``` 这样,你就可以在Vue.js项目中实现每隔5执行一次的功能,同时避免了页面卡死的问题。 #### 引用[.reference_title] - *1* *2* [vue项目使用定时器每隔几请求一次接口](https://blog.csdn.net/qq_39840470/article/details/104635153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [vue项目使用定时器每隔几运行一次某方法](https://blog.csdn.net/wulikunbing/article/details/127746447)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值