import store from '@/store'; const exam = { namespaced: true, state: { started: false, //考试状态 usedTime: 0, //考试当前所用时间 totalTime: 0, //考试总时间 timeInterval: null, //计时器 suspend: false, //暂停 }, getters: { started: (state) => { return state.started; }, usedTime: (state) => { return state.usedTime; }, totalTime: (state) => { return state.totalTime; }, suspend: (state) => { return state.suspend; } }, mutations: { setStarted: (state, started) => { state.started = started; }, setSuspend: (state, suspend) => { state.suspend = suspend; }, setUsedTime: (state, usedTime) => { state.usedTime = usedTime; }, setTotalTime: (state, totalTime) => { state.totalTime = totalTime; }, countUsedTime: (state) => { if (state.timeInterval) { clearInterval(state.timeInterval); state.timeInterval = null; } state.timeInterval = setInterval(() => { if (!state.suspend) { state.usedTime++; } }, 1000); }, stopCountTime: (state) => { if (state.timeInterval) { clearInterval(state.timeInterval); state.timeInterval = null; } } }, actions: { start({ commit }) { commit('setStarted', true); commit('setSuspend', false); commit('setUsedTime', 0); commit('countUsedTime'); }, over({ commit }) { commit('setStarted', false); commit('setSuspend', true); commit('setUsedTime', 0); commit('stopCountTime'); }, isOver() { let trainingList = store.getters['trainingList/trainingList']; let doList = trainingList.filter(elem => { if (elem.usedTime !== undefined) return true; }); if (doList.length >= trainingList.length) { return true; } }, setSuspend({ commit }, suspend) { commit('setSuspend', suspend); }, countUsedTime({ commit }) { commit('countUsedTime'); }, setUsedTime({ state }, usedTime) { if (usedTime) { state.usedTime = usedTime; } }, setTotalTime({ commit }, totalTime) { if (totalTime) { commit('setTotalTime', totalTime); } } } }; export default exam;