Force some js codes rerun on each card back when using Anki Desktop

Hello everyone, have a nice day!
I have some js codes to get the audio tag and play it, however, it works only on the first card, and the next cards always play the same audio from the first card.
I searched and knew that Anki desktop will keep a single web view and thus my js codes only run the first time. How can I force my js codes to rerun each time for a new card?
My code likes:

<audio id="audioreplay" src="{{myaudio}}.mp3"></audio>

var replayer = document.getElementById("audioreplay");

replayer.src = "{{myaudio}}.mp3";;


I also tried something like (function () { ... })(); with no lucky.
Thanks in advance!

This works for me:

<audio id="audioreplay" src="{{myaudio}}.mp3"></audio>

    (() => {
        var replayer = document.getElementById("audioreplay");

        replayer.src = "{{myaudio}}.mp3";;

Hi abdo, thanks for your reply.
I forget to mention that my deck is created by other people, and there remains a long js code in the same tag after my code pasted before.
Today I tried to delete the long js code, and my audio play js can be executed once every card.

I don’t know why my js is not executed when there exists other js codes. Is there a workaround I can keep both of the two js?

Share a sample deck

Hi abdo, here it is:
The Card 3 type is which one has both js codes.
Btw, our forum can not attach a file, can it?

The code works for me even with that.

I think only images are allowed.

1 Like

I just want to make sure, do you hearing that the audio automatically plays in a loop for Card 3 type?
and there is a console.log('------- d1.0'); in my js, does it print once for each card?

You remind me that maybe I should upgrade my Anki desktop version, and currently it is:

Anki 2.1.56 (07fd88dd) Python 3.9.15 Qt 6.3.2 PyQt 6.3.1
Platform: macOS-12.6-x86_64-i386-64bit

I use 2.1.56 because I use anki-sync-server, a third-party tools which only supports to that version currently. I will upgrade Anki sometime later.

Thanks for your time.

Hmm, the audio plays in a loop but the text is only printed once. It’s caused by something in the existing code apparently but it’s hard to debug since it’s minified/obfuscated.

1 Like

Yes, it’s complicated, and I am totally unfamiliar with js/jquery…
Thanks again.

Hi @abdo, I find a workaround: using <script defer> ....</script> to enclose my js, and then these js codes will be executed every card.
Thanks again and have a nice day!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.