JS code works on new post but doesn’t when updating
-
Hello,
I’m having a problem with my JS code. It works when I’m adding a new post but when I save it and insert a new post, it doesn’t.
What my JS code does is when I insert a file, it reads the file size and time length. On a new file, when it loads an MP3 file, it will output the file size and time length.
Example: when adding tms_1527.mp3, it outputs
File Size: 118598822
Song Duration: 02:03:24If I add new file on a saved post, it will read the file size but doesn’t output the time of the file.
Example: when adding tms_1471.mp3, it outputs
File Size: 92882726
Song Duration:JS Code:
function metabox_mp3_analyze() { add_meta_box('mp3_start_box', 'Analyze MP3', 'mp3_start_box', 'post', 'normal', 'default'); }; add_action( 'add_meta_boxes', 'metabox_mp3_analyze' ); function mp3_start_box(){ ?> <style type="text/css"> audio {display: none} </style> <script type="text/javascript"> jQuery(function(){ var objectUrl; jQuery("#audio").on("canplaythrough", function(e){ var seconds = e.currentTarget.duration; var duration = moment.duration(seconds, "seconds"); var hours = duration.hours(); var mins = duration.minutes(); var secs = duration.seconds(); if (hours < 10) { time_hour = "0" + hours + ":" ; } else { time_hour = hours + ":" ;}; if (mins < 10) { time_mins = "0" + mins + ":" ; } else { time_mins = mins + ":" ;}; if (secs < 10) { time_secs = "0" + secs} else { time_secs = secs}; jQuery("#duration").text(time_hour+time_mins+time_secs); jQuery("#timelength").val(time_hour+time_mins+time_secs); URL.revokeObjectURL(objectUrl); }); jQuery("#file").change(function(e){ var file = e.currentTarget.files[0]; jQuery("#filesize").text(file.size); jQuery("#size").val(file.size); objectUrl = URL.createObjectURL(file); jQuery("#audio").prop("src", objectUrl); }); }); </script> <input type="file" id="file" /> <audio id="audio"></audio> <p> <label>File Size:</label> <span id="filesize"></span> <input type="hidden" id="size" name="size" value=""/> </p> <p> <label>Song Duration:</label> <span id="duration"></span> <input type="hidden" id="timelength" name="time" value=""/> </p> <?php }; add_action('wp_head','mp3_start_box');
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘JS code works on new post but doesn’t when updating’ is closed to new replies.