[請益] vue js用變數呼叫function

看板Soft_Job作者 (500年沒換暱稱了)時間7年前 (2018/12/11 15:51), 7年前編輯推噓9(906)
留言15則, 8人參與, 7年前最新討論串1/1
請教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

12/11 16:03, 7年前 , 1F
所以你的問題是:如何依照用戶自訂的順序去執行功能嗎?
12/11 16:03, 1F
非常精闢,感謝,哈哈,是的,簡單說就是DCT大大的結論 XD

12/11 16:04, 7年前 , 2F
不對 你 action array裡面用的是字串
12/11 16:04, 2F

12/11 16:04, 7年前 , 3F
應該要 action [ this.action1, this.action2, this.a
12/11 16:04, 3F

12/11 16:04, 7年前 , 4F
ction3 ]
12/11 16:04, 4F

12/11 16:06, 7年前 , 5F
然後記得 settimeout不是非同步function
12/11 16:06, 5F
謝謝,settimeout是async這件事是知道的 所以一定要存進actions就設定完整的function name囉? ※ 編輯: asleepme (223.136.67.63), 12/11/2018 16:15:06

12/11 17:41, 7年前 , 6F
是啊 你要在action array 塞function的 reference 他
12/11 17:41, 6F

12/11 17:41, 7年前 , 7F
才知道指引到哪一個
12/11 17:41, 7F

12/11 20:09, 7年前 , 8F
this[action[0]]()
12/11 20:09, 8F

12/11 22:34, 7年前 , 9F
樓上厲害
12/11 22:34, 9F

12/12 00:44, 7年前 , 10F
this可以這樣包methods function!?(大驚!?
12/12 00:44, 10F

12/12 08:57, 7年前 , 11F
就取thos prop然後執行啊
12/12 08:57, 11F

12/12 09:02, 7年前 , 12F
*this
12/12 09:02, 12F

12/12 12:26, 7年前 , 13F
user設定method array 走訪array去執行method 依序等待
12/12 12:26, 13F

12/12 13:37, 7年前 , 14F
感覺你需要像 redux-saga之類的套件
12/12 13:37, 14F

12/23 14:16, 7年前 , 15F
rxjs ,保證處理的省力很多
12/23 14:16, 15F
文章代碼(AID): #1S3snism (Soft_Job)