如何动态地创建一个JavaScript的sessionStorage / HTML5中的数组 [英] How to dynamically create an array within a javascript sessionstorage / HTML5

查看:382
本文介绍了如何动态地创建一个JavaScript的sessionStorage / HTML5中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图通过JavaScript播放声音,并想用的sessionStorage动态地改变它。

下面是简化版本,在Android的/ FF的Linux / Win中播放声音/ S当u点击雪碧我的按钮 - 我已经包括了其他按钮的例子来设置,并在HTML5中检索会话值<。 / p>

http://globability.org/webapp/asprite20111124_8.html

下面的wiki有Android手机的规格,其中它的工作原理:(三星Galaxy SII)的情况下,你有兴趣

http://globability.org/wiki/doku.php?id= current_working_specs_p标签 /又见 http://globability.org/wiki/doku的.php?ID = mobile_pointing_tablet 以获得正确的想法有关它是什么,我的工作。

我需要的是有玩soundsprite的JavaScript,你可以看到从sessionStorage的加载的sessionStorage和插入值插入到一个数组以下部分负荷之下。

我不找任何的变化是声音的播放方式 - 只需要在特定的JavaScript内作出动态构建阵列工作

在code下面是基于www.phpied.com/audio-sprites/由斯托扬斯特凡的soundsprite想法 - 为减少需要播放声音的HTTP调用......还有稳定的音质,少波涛汹涌的声音等。

在这里Antway有云:你只需要看看伪code型钢 - 其余的工作。

 &LT;脚本&GT;
VAR的事情='东西';功能关闭(){
如果(typeof运算thing.pause!==未定义){
    thing.pause();
}
}功能日志(什么){
的document.getElementById('登录')的innerHTML + =什么+&LT; BR&gt;中。
}VAR spriteme =功能(){
VAR精灵= {
// ID:[开始,长度]'空白':[0.1,0.5],//这第一子画面,它必须被定义的第一个和是
第一次调用,它是一个坯件中的声音组合的声音文件,需要为
现在。'成功':[13,2,5]/ *我想是能够即设置参数叮声动态播放 -
这里使用preparation会话存储用于获取声音伪code例子
从数据库中的参数
wordgen'[NULL,NULL]
//这个数组应该从两个会话存储密钥不知道你需要HTML5的新事物读值来动态构建sessionStorage.globabilitykey1;
sessionStorage.globabilitykey2;strkey1 = globabilitykey1
strkey2 = globabilitykey2VAR gkey1 = parsefloat(strkey1)
VAR gkey2 = parsefloat(strkey2)wordgen':[gkey1,gkey2]
然后这个想法是用生成,以取代在脚本阵列成功
阵'wordgen,让声音的动态SETING播放* ///下面是从soundsprites收集的网站从播放声音叮咬'字1':[0.5,2,36],// 1
WORD2':[3.1,3.0],// 2
WORD3':[7.0,1.82],// 3
'word4':[10.03,2],// 4?},
歌= ['空白','成功'],
//这里你设置播放顺序(这是我想用'wordgen'来代替'成功'
电流= 0,
ID =歌曲[当前]
开始= 0,
最终=精灵[ID] [1]
INT;事情=的document.getElementById('雪碧');
thing.play();日志('文件:'+ thing.currentSrc);
日志(ID +':启动:'+精灵[ID]。加入(',长度:'));//改变
INT =的setInterval(函数(){
如果(thing.currentTime&GT;结束){
thing.pause();
如果(当前=== song.length - 1){
clearInterval(INT);
返回;
}
当前++;
ID =歌曲[现行];
开始=精灵[ID] [0];
最终=启动+精灵[ID] [1]
thing.currentTime =启动;
thing.play();
日志(ID +':启动:'+精灵[ID]。加入(',长度:'));
}
},10);
};
&LT; / SCRIPT&GT;

这是如何动态地基于价值的JavaScript中创建了wordgen阵列任何想法是sessionStorage的?

整个code /工作的例子可以看这里: http://globability.org/ Web应用程序/ asprite20111124_8.html

我知道页面一个丑陋的烂摊子......但是这是一个alpha原型:)


解决方案

哎呀......这是SOOOOO简单,我是如此接近解决它自己,但是在freelancer.com一种自由职业者谁也perviously帮助我得到一个我的想法来工作,这一次再次做到了...

和事不宜迟:

wordgen':的eval([+ sessionStorage.globabilitykey1 +,+ sessionStorage.globabilitykey2 +]),

这是我需要的东西 - 我一直在尝试做相同的,但没有的评估前在 paranthesis 围绕参数....

哦,不要忘记通过点击该按钮尝试之前设置的值还是会有没有声音出来的扬声器;)

下面是到工作页面的链接,如果你愿意尝试,如果你想看到的code在它的条款内容:的 http://globability.org/webapp/asprite20111201_2.html - 感谢那些你投票的问题了!

Trying to play a sound via javascript and want to change it dynamically using sessionstorage

The following is a simplified version that plays sound/s in android/FF Linux/Win when u click the "sprite me button" - I've included other buttons for the example to set and retrieve session values in HTML5.

http://globability.org/webapp/asprite20111124_8.html

The wiki below has Android phone specs where it works: (Samsung Galaxy SII) in case you're interested

http://globability.org/wiki/doku.php?id=current_working_specs_p-tab / also see http://globability.org/wiki/doku.php?id=mobile_pointing_tablet to get a proper idea about what it is that I am working on.

What I need is to have the "play soundsprite" javascript that you can see below in the following section load from sessionstorage and insert values loaded from sessionstorage inserted into an array.

I am not looking for any changes is how the sound is played back - just need to make a dynamically built array work from within the particular javascript.

The code below is based on the soundsprite idea from www.phpied.com/audio-sprites/ by Stoyan Stefanov - made to reduce the http calls needed for playing sounds... Also stabilizes the sound quality, less choppy sound etc.

Antway here goes: YOU ONLY NEED TO LOOK AT PSEUDOCODE SECTION - The rest is functioning

<script>
var thing = 'the thing';

function shut() {
if (typeof thing.pause !== 'undefined') {
    thing.pause();
}
}

function log(what) {
document.getElementById('log').innerHTML += what + "<br>";
}

var spriteme = function(){
var sprites = {
// id: [start, length]

'blank':[0.1, 0.5], //This the first sprite, it has to be the first defined and is 
called first, it is a blank piece of sound in the combined sound file and needed as of 
now.

'success':[13,  2,5],

/* I would like to be able to set the parameters i.e. sound bite to play dynamically - 
here a pseudocode example using session storage in preparation for getting the sound 
parameters from a database


'wordgen'[null,null]; 
//this array should be dynamically built from values read from the two session storage keys not sure you need the new thing in HTML5

sessionStorage.globabilitykey1;
sessionStorage.globabilitykey2;

strkey1=globabilitykey1
strkey2=globabilitykey2

var gkey1=parsefloat(strkey1)
var gkey2=parsefloat(strkey2)

'wordgen':[gkey1,gkey2]


and then the idea is to replace the array success in the script with the "generated" 
array 'wordgen' to allow dynamic seting of sound to play back  */       

//the following are sound bites from the collection of soundsprites the site plays from

'word1': [0.5, 2,36], //one
'word2': [3.1,  3.0], //two
'word3': [7.0,  1.82], //three
'word4': [10.03, 2], //four ?

},
song = ['blank', 'success'], 
//here you're setting the playback sequence (this is where I would like to replace 'success' with 'wordgen'
current = 0,
id = song[current],
start = 0,
end = sprites[id][1],
int;

thing = document.getElementById('sprite');
thing.play();

log('file: ' + thing.currentSrc);
log(id + ': start: ' + sprites[id].join(', length: '));

// change
int = setInterval(function() {
if (thing.currentTime > end) {
thing.pause();
if (current === song.length - 1) {
clearInterval(int);
return;
}
current++;
id = song[current];
start = sprites[id][0];
end = start + sprites[id][1]
thing.currentTime = start;
thing.play();
log(id + ': start: ' + sprites[id].join(', length: '));
}
}, 10);   
};
</script>

Any ideas on how to dynamically create the 'wordgen' array within the javascript based on the values is sessionstorage ?

The whole code/working example can be seen here: http://globability.org/webapp/asprite20111124_8.html

I know the page is one ugly mess... but this is an alpha prototype :)

解决方案

Argh... it was sooooo simple, I was this close to solving it myself, however a kind freelancer at freelancer.com who has perviously helped me getting one of my ideas to work did it again this time...

And without further ado:

'wordgen':eval("["+sessionStorage.globabilitykey1+","+sessionStorage.globabilitykey2+"]"),

That was what I needed - I had been trying to do the same but without the " eval " in front and the paranthesis around the arguments....

Oh and don't forget to set the value by clicking the button before you try or there will be no sound coming out of your speakers ;)

Here's a link to the working page if you care to try and if you want to see the code in it's "entirity": http://globability.org/webapp/asprite20111201_2.html - Thanks to those of you voting the question up!!!

这篇关于如何动态地创建一个JavaScript的sessionStorage / HTML5中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆