Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

怎么判断addeventlistener('message')和取到的localstorage

项目需求是进入页面addeventlistener('message'),接收到数据拿着数据调用接口,如果没有并且本地有数据,则用本地的,但是现在问题就是肯定优先使用message传入数据,没有采用本地的(例如刷新)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

怎么会触发这个场景呢?

data = localstorage.get('data');
addeventlistener('message',function(){
    data = localstorage.get('data');
})

这样不就是优先使用message的吗。如果他触发了,就是使用message的。(我上面的代码跑不起来,但是意思是一样的


message 是什么事件呀?postMessage过来的嘛?

localStorage.getItem('data')
localStorage.setItem('data', 2)

window.addEventListener('message',(...args)=>console.log('message', args))

window.addEventListener('storage',(...args)=>console.log('storage', args))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...