###前提・実現したいこと
いつもお世話になっております。
jqueryを使い、一定時間ごとにHTMLの該当箇所のpositionの値(top)を取得し、if文の条件式の変数に利用しようと考えています。
【教えて頂きたい内容】
該当箇所のpositionの値(top)をsetInterval()で一定時間ごとにconsole.log()で確認する事までは出来ましたが、
別functionを使い、clickイベント後のif文の条件式として使用したいため、引数として、setInterval()のfunctionから出したいと考えていますが、エラーが発生し出来ません。
ご教授願います。
###発生している問題・エラーメッセージ
javascript
1 2 (function(){ 3// 1秒毎にpositionのtopの値を取得 4 var timerID = setInterval(function(){ 5 $(".target").each(function positionDataGet(){ 6 7 // 下記consoloe.log()で値の取得が確認出来たため、変数eachPositionを作成し、 8 //returnでpositionDataGetのfunctionの引数として戻しています。 9 10 console.log(parseInt($(this).css("top"))); 11 var eachPosition =parseInt($(this).css("top")); 12 return eachPosition; 13 }); 14 15 //下記でsetInterval()のfunctionから外に出すため、引数の確認をしていますが、 16 // そこでエラーが発生してしまい、対応出来ない状況です。 17 var x = positionDataGet(); 18 console.log(positionDataGet()); 19 20 },1000); 21 })(); 22
###該当のソースコード
html
1ここにご自身が実行したソースコードを書いてください 2下記divの3つあるtargetの各positionを1秒毎に取得し、別のイベント(click)のif文の条件式の変数に利用したいと考えています。 3 <div class="target"> 4 <label> 5 <input type="radio" name="target1" value="0" checked="checked"> 6 <img class="ball" src="img/fireworks_game_ver1.0/fireworks/tama.png"> 7 </label> 8 <div class="smoke"> 9 <img src="img/fireworks_game_ver1.0/fireworks/smork.png"> 10 </div> 11 </div> 12 <div class="target"> 13 <label> 14 <input type="radio" name="target2" value="0" checked="checked"> 15 <img class="ball" src="img/fireworks_game_ver1.0/fireworks/tama.png"> 16 </label> 17 <div class="smoke"> 18 <img src="img/fireworks_game_ver1.0/fireworks/smork.png"> 19 </div> 20 </div> 21 <div class="target"> 22 <label> 23 <input type="radio" name="target3" value="0" checked="checked"> 24 <img class="ball" src="img/fireworks_game_ver1.0/fireworks/tama.png"> 25 </label> 26 <div class="smoke"> 27 <img src="img/fireworks_game_ver1.0/fireworks/smork.png"> 28 </div> 29 </div>
javascript
1 (function(){ 2 var timerID = setInterval(function(){ 3 $(".target").each(function positionDataGet(){ 4 console.log(parseInt($(this).css("top"))); 5 var eachPosition =parseInt($(this).css("top")); 6 return eachPosition; 7 }); 8 var x = positionDataGet(); 9 console.log(positionDataGet()); 10 },1000); 11 })();
###試したこと
ネットや本サイトで同様の質問や回答がないか探しましたが、上手く見つけられなったため、
教えて頂きたいです。
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/10 12:41