報錯內容如下
[JS Framework] Failed to find taskCenter (35).
[JS Framework] Failed to execute the callback function:TypeError: c.clearTimeout is not a function
reportJSException >>>> exception function:__WEEX_CALL_JAVASCRIPT__, exception:JavaScript execute error!Uncaught TypeError: c.clearTimeout is not a function
報錯代碼如下
<script setup lang="ts">
const videoHeight = ref(0);
const videoWidth = ref(0);
onLoad(() => {plus.screen.lockOrientation('landscape-primary');
});
function handleWindowResize(res) {videoWidth.value = res.size.windowWidth;videoHeight.value = res.size.windowHeight;
}
onMounted(() => {uni.onWindowResize(debounce(handleWindowResize, 200))
})
onBeforeUnmount(() => {uni.offWindowResize(handleWindowResize)
})// debounce函數定義
function debounce(fn: Function, wait: number) {let timer: any;return function () {timer && clearTimeout(timer);timer = setTimeout(() => {timer = false;fn.apply(this, arguments); // 把參數傳進去}, wait);};
}
</script>
進入頁面,切換橫屏獲取寬高設置video組件占滿全屏,監聽橫豎屏切換防抖避免多次重復賦值。從開發流程上沒啥問題onWindowResize和offWindowResize就是用來解決這個問題的。搜了一些帖子也沒搜到相關答案,本想著實在不行就把防抖取消掉也不是不能用。今天有空翻了下官方文檔看到一個沒用過的?生命周期?onResize?覺得也可以解決此問題就試了一下。
改后的代碼如下
<script setup lang="ts">
import { onResize } from '@dcloudio/uni-app'
const videoHeight = ref(0);
const videoWidth = ref(0);
onLoad(() => {plus.screen.lockOrientation('landscape-primary');
});
function handleWindowResize(res) {videoWidth.value = res.size.windowWidth;videoHeight.value = res.size.windowHeight;
}
onResize(debounce(handleWindowResize, 200))// debounce函數定義
function debounce(fn: Function, wait: number) {let timer: any;return function () {timer && clearTimeout(timer);timer = setTimeout(() => {timer = false;fn.apply(this, arguments); // 把參數傳進去}, wait);};
}
</script>
棄用onWindowResize和offWindowResize改用生命周期,不僅更加簡單方便而且解決了我的問題