看板 Soft_Job 關於我們 聯絡資訊
請教js大大們,有個應用是要依序執行3個動作 action1, aciton2, action3 每個動作間會有delay, delay1, delay2, delay3 action的順序是讓user可以設定的ex: action1,2,3 or action3,1,2 這樣我是不是可以把user設定好的順序存成一個array叫actions好了 然後value剛好就是依序要執行的action的function name actions = [ 'action3', 'action2', 'action1']; 然後像這樣用 actions[0](); 但是每個action是寫在vue的methods裡面,所以實際上要執行action會像這樣 this.action1(); 如果想用變數的方式處理,就會變成 currentAction = actions[0]; this.currentAction() 顯然這樣是不可行的 有什麼辦法可以處理這個問題呢? 另外每個動作間的delay我是用setTimeout來處理 感覺看起來很不直覺,不知道有更好的辦法嗎? 感謝~ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.136.67.63 ※ 文章網址: https://www.ptt.cc/bbs/Soft_Job/M.1544514668.A.DB0.html
DCTmaybe: 所以你的問題是:如何依照用戶自訂的順序去執行功能嗎? 12/11 16:03
非常精闢,感謝,哈哈,是的,簡單說就是DCT大大的結論 XD
a510042003: 不對 你 action array裡面用的是字串 12/11 16:04
a510042003: 應該要 action [ this.action1, this.action2, this.a 12/11 16:04
a510042003: ction3 ] 12/11 16:04
a510042003: 然後記得 settimeout不是非同步function 12/11 16:06
謝謝,settimeout是async這件事是知道的 所以一定要存進actions就設定完整的function name囉? ※ 編輯: asleepme (223.136.67.63), 12/11/2018 16:15:06
a510042003: 是啊 你要在action array 塞function的 reference 他 12/11 17:41
a510042003: 才知道指引到哪一個 12/11 17:41
Phenomenon: this[action[0]]() 12/11 20:09
TitanEric: 樓上厲害 12/11 22:34
DCTmaybe: this可以這樣包methods function!?(大驚!? 12/12 00:44
fukinhot: 就取thos prop然後執行啊 12/12 08:57
fukinhot: *this 12/12 09:02
max241: user設定method array 走訪array去執行method 依序等待 12/12 12:26
hstt: 感覺你需要像 redux-saga之類的套件 12/12 13:37
truthmanman: rxjs ,保證處理的省力很多 12/23 14:16